1. 程式人生 > >c# 多執行緒使用佇列順序寫日誌的類 (需要再優化)

c# 多執行緒使用佇列順序寫日誌的類 (需要再優化)

using System;
using System.Collections.Generic;
using System.Threading;

public  class LogManager
{
    /// <summary>
    /// 建構函式
    /// </summary>
    static LogManager ()
    {
        Start();
    }

    #region 佇列方法

    /// <summary>
    /// 日誌佇列
    /// </summary>
    private static Queue<Log> ListQueue = new Queue<Log>();

    class Log
    {
        public string File { get; set; }
        public string Msg { get; set; }
    }


    public static void WriteLog(string logFile, string msg)
    {
        Log log = new Log()
        {
            File = logFile,
            Msg = msg
        };

        ListQueue.Enqueue(log);
    }

    private static void Start()//啟動
    {

        WriteLog("ULog", "Start");
        Thread thread = new Thread(threadStart);
        thread.IsBackground = true;
        thread.Start();

    }

    private static void threadStart()
    {
        while (true)
        {
            if (ListQueue.Count > 0)
            {
                try
                {
                    ScanQueue();
                }
                catch (Exception ex)
                {
                    throw;
                    //LO_LogInfo.WLlog(ex.ToString());
                }
            }
            else
            {
                //沒有任務,休息3秒鐘
                Thread.Sleep(1000);
            }
        }
    }
    //要執行的方法
    private static void ScanQueue()
    {
        while (ListQueue.Count > 0)
        {
            try
            {
                //從佇列中取出
                Log log = ListQueue.Dequeue();
                ThreadLog(log.File, log.Msg);

                //Console.WriteLine(queueinfo.feedid);
                //取出的queueinfo就可以用了,裡面有你要的東西
                //以下就是處理程式了
                //。。。。。。

            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }

    #endregion

    private static string logPath = string.Empty;

    /// <summary>
    /// 儲存日誌的資料夾
    /// </summary>
    public static string LogPath
    {
        get
        {
            if (logPath == string.Empty)
            {
                if (System.Web.HttpContext.Current == null)
                    // Windows Forms 應用
                    logPath = AppDomain.CurrentDomain.BaseDirectory + @"Logs\";
                else
                    // Web 應用
                    logPath = AppDomain.CurrentDomain.BaseDirectory + @"Logs\";
            }
            return logPath;
        }
        set { logPath = value; }
    }

    private static string logFielPrefix = string.Empty;
    /// <summary>
    /// 日誌檔案字首
    /// </summary>
    public static string LogFielPrefix
    {
        get { return logFielPrefix; }
        set { logFielPrefix = value; }
    }


    /// <summary>
    /// 寫日誌
    /// </summary>
    private  static void ThreadLog(string logFile, string msg)
    {
        try
        {
            System.IO.StreamWriter sw = System.IO.File.AppendText(
                LogPath + DateTime.Now.ToString("yyyyMMdd") +
                LogFielPrefix + " " + logFile + ".Log"

                );
            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff : ") + msg);
            sw.Close();
        }
        catch
        { }
    }

    /// <summary>
    /// 寫日誌
    /// </summary>
    public static void WriteLog(LogFile logFile, string msg)
    {
        WriteLog(logFile.ToString(), msg);
    }
}

/// <summary>
/// 日誌型別
/// </summary>
public enum LogFile
{
    Trace,
    Warning,
    Error,
    SQL
}