2017年8月23日 星期三

C# 好用的 Log 紀錄 Class

這是一個用來記錄 Log的Class,只需要設定 filePath 大致上就可以運作了

public static class TraceLog
    {
        private static object _lockLog = new object();
        private static int _numberOfLines = 0;

        public static string filePath = @"C:\MyService_LOG_DIR";
        public static string serviceName = "MyService";        // 服務名稱
        public static int maxLines = 65536;      // Log 行數限制
        public static int traceLevel = 3;        // value is 1~9

        public static void Write(int pTraceLevel, string pSource, string message)
        {
            if (pTraceLevel <= traceLevel)
                Write(pSource + ":" + message);
        }
        public static void Write(string format, params object[] message)
        {
            Write(string.Format(format, message));
        }
        public static void Write(string message)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                filePath = Directory.GetCurrentDirectory();
            }

            // 選擇檔案名稱
            string filename = filePath + string.Format("\\{0:yyyy}\\{0:MM}\\{1}.{0:yyyy-MM-dd}.txt",
                                                                    DateTime.Now,
                                                                    serviceName);
            FileInfo finfo = new FileInfo(filename);

            if (finfo.Directory.Exists == false)
            {
                finfo.Directory.Create();
            }

            if (_numberOfLines == 0)
                if (File.Exists(filename))
                    _numberOfLines = File.ReadAllLines(filename).Length;
                else
                    _numberOfLines = 0;
            if (_numberOfLines >= maxLines)
            {
                // 檔案夾搬移
                for (int i = 10; i > 0; i--)
                {
                    string currFile = filename.Substring(0, filename.Length - 4) + "." + i.ToString() + ".txt";
                    if (File.Exists(currFile))
                    {
                        if (i == 10)
                        {
                            File.Delete(currFile);
                            continue;
                        }

                        FileInfo f = new FileInfo(currFile);
                        f.MoveTo(currFile.Substring(0, filename.Length - 4) + "." + (i + 1).ToString() + ".txt");
                    }
                }

                // 重新計算行數
                finfo.MoveTo(filename.Substring(0, filename.Length - 4) + ".1.txt");
                _numberOfLines = 0;
            }


            // 訊息格式化
            string writeString = string.Format("{0:yyyy/MM/dd HH:mm:ss.ff} {1} {2}",
                                                DateTime.Now,
                                                serviceName + "." + System.Threading.Thread.CurrentThread.ManagedThreadId,
                                                message) + Environment.NewLine;
            // 寫Log前,先Lock
            lock (_lockLog)
            {
                File.AppendAllText(filename, writeString, Encoding.Unicode);
            }
            _numberOfLines++;
        }
    }

沒有留言:

張貼留言