c#簡易log

yellowlee發表於2008-12-29
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace database
{
    class DebugLog
    {
        #region common define
        private static string LOGFILE = "logfile.log";
        private static string APPLOGINFO = "應用程式啟動";
        private static string APPLOGCLOSE = "應用程式關閉";
        private static string APPLOGSEPARATOR = " ||... ";
        private static string APPLOGFOOTER = "--------------------------------------------------";
        private static StreamWriter sw;
        #endregion

        //設定字符集
        public static void setEncoing()
        {
            //sw.Encoding = System.Text.Encoding.Unicode;
        }

        //建立應用程式日誌
        public static void CreateAppLog()
        {
            if (File.Exists(LOGFILE))
            {
                sw = File.AppendText(LOGFILE);
            }

            else
            {
                sw = File.CreateText(LOGFILE);
            }

            sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + APPLOGINFO);
            CloseLog();
        }

        //建立執行時日誌
        public static void CreateLog()
        {
            if (File.Exists(LOGFILE))
            {
                sw = File.AppendText(LOGFILE);
            }

            else
            {
                sw = File.CreateText(LOGFILE);
            }

        }

        //關閉應用程式日誌
        public static void CloseAppLog()
        {
            CreateLog();
            sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + APPLOGCLOSE);
            sw.WriteLine(APPLOGFOOTER);
            sw.Close();
        }

        //關閉執行時日誌
        public static void CloseLog()
        {
            sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + APPLOGCLOSE);
            sw.Close();
        }

        //寫log obj
        public static void WriteLog(object obj)
        {
            string str = obj.ToString();

            CreateLog();

            if (File.Exists(LOGFILE))
            {
                sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
            }

            else
            {
                sw = File.CreateText(LOGFILE);
                sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
            }

            sw.Close();

        }

        //寫log string
        public static void WriteLog(string str)
        {

            CreateLog();

            if (File.Exists(LOGFILE))
            {
                sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
            }

            else
            {
                sw = File.CreateText(LOGFILE);
                sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
            }

            sw.Close();

        }

        //寫log obj
        public static void Debuglog(object ob)
        {
            WriteLog(ob);
        }

        //寫錯誤日誌
        public static void Debuglog(Exception e)
        {
            WriteLog(e.Message);
        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16179598/viewspace-524530/,如需轉載,請註明出處,否則將追究法律責任。

相關文章