C# 日誌文件存儲
-
思路:直接對txt文件內容進行存儲即可。
-
存儲時,手動設置存儲內容。
-
核心程序
/// <summary> /// 日誌系統 /// </summary> public class LogAct { /// <summary> /// 普通日誌 /// </summary> /// <param name="className">類名</param> /// <param name="info">日誌記錄</param> public static void Info(string className, string info) { WriteLog("INFO", className, info); } /// <summary> /// 警告日誌 /// </summary> /// <param name="className">類名</param> /// <param name="info">日誌記錄</param> public static void Warn(string className, string info) { WriteLog("WARN", className, info); } /// <summary> /// 錯誤日誌 /// </summary> /// <param name="className">類名</param> /// <param name="info">日誌記錄</param> public static void Error(string className, string info) { WriteLog("ERROE", className, info); } /// <summary> /// 鎖對象【支持多線程訪問】 /// </summary> static object lockObject = new object(); /// <summary> /// 寫入日誌 /// </summary> /// <param name="className">類名</param> /// <param name="infoLevel">日誌級別</param> /// <param name="info">日誌記錄</param> private static void WriteLog(string className, string infoLevel, string info) { lock (lockObject) { string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "/logs"; if (!Directory.Exists(logFilePath)) { Directory.CreateDirectory(logFilePath); } string logFileName = logFilePath + "/" + "log_" + DateTime.Now.ToString("yyyyMMdd") + ".log"; if (!File.Exists(logFileName)) { File.Create(logFileName).Close(); } string logFormat = string.Format("[ {0} ] {1} {2} {3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"), className, infoLevel, info); using (StreamWriter sw= File.AppendText(logFileName)) { sw.WriteLine(logFormat); sw.Flush(); sw.Close(); } } } }
-
多線程測試代碼
Task.Run(() => { for (int i = 0; i < 20; i++) { LogAct.Info(typeof(App).Name, "程序已經啓動"+i); } }); Task.Run(() => { for (int i = 0; i < 20; i++) { LogAct.Info(typeof(App).Name, "-----85程序已經啓動" + i); } });
-
OK。功能完成。
-
日誌效果
[ 2020-03-23 16:59:02 587 ] INFO App 程序已經啓動0 [ 2020-03-23 16:59:02 614 ] INFO App -----85程序已經啓動0 [ 2020-03-23 16:59:02 618 ] INFO App -----85程序已經啓動1 [ 2020-03-23 16:59:02 623 ] INFO App 程序已經啓動1 [ 2020-03-23 16:59:02 628 ] INFO App 程序已經啓動2 [ 2020-03-23 16:59:02 633 ] INFO App -----85程序已經啓動2 [ 2020-03-23 16:59:02 641 ] INFO App -----85程序已經啓動3 [ 2020-03-23 16:59:02 647 ] INFO App 程序已經啓動3 [ 2020-03-23 16:59:02 655 ] INFO App 程序已經啓動4 [ 2020-03-23 16:59:02 660 ] INFO App -----85程序已經啓動4 [ 2020-03-23 16:59:02 666 ] INFO App -----85程序已經啓動5 [ 2020-03-23 16:59:02 672 ] INFO App 程序已經啓動5 [ 2020-03-23 16:59:02 679 ] INFO App 程序已經啓動6 [ 2020-03-23 16:59:02 687 ] INFO App -----85程序已經啓動6 [ 2020-03-23 16:59:02 692 ] INFO App -----85程序已經啓動7 [ 2020-03-23 16:59:02 696 ] INFO App 程序已經啓動7 [ 2020-03-23 16:59:02 701 ] INFO App 程序已經啓動8 [ 2020-03-23 16:59:02 709 ] INFO App -----85程序已經啓動8 [ 2020-03-23 16:59:02 716 ] INFO App -----85程序已經啓動9 [ 2020-03-23 16:59:02 724 ] INFO App 程序已經啓動9 [ 2020-03-23 16:59:02 732 ] INFO App -----85程序已經啓動10 [ 2020-03-23 16:59:02 744 ] INFO App 程序已經啓動10 [ 2020-03-23 16:59:02 749 ] INFO App 程序已經啓動11 [ 2020-03-23 16:59:02 754 ] INFO App 程序已經啓動12 [ 2020-03-23 16:59:02 759 ] INFO App -----85程序已經啓動11 [ 2020-03-23 16:59:02 764 ] INFO App -----85程序已經啓動12 [ 2020-03-23 16:59:02 768 ] INFO App -----85程序已經啓動13 [ 2020-03-23 16:59:02 773 ] INFO App 程序已經啓動13 [ 2020-03-23 16:59:02 778 ] INFO App 程序已經啓動14 [ 2020-03-23 16:59:02 782 ] INFO App 程序已經啓動15 [ 2020-03-23 16:59:02 787 ] INFO App -----85程序已經啓動14 [ 2020-03-23 16:59:02 796 ] INFO App -----85程序已經啓動15 [ 2020-03-23 16:59:02 802 ] INFO App 程序已經啓動16 [ 2020-03-23 16:59:02 809 ] INFO App 程序已經啓動17 [ 2020-03-23 16:59:02 814 ] INFO App -----85程序已經啓動16 [ 2020-03-23 16:59:02 819 ] INFO App -----85程序已經啓動17 [ 2020-03-23 16:59:02 828 ] INFO App 程序已經啓動18 [ 2020-03-23 16:59:02 833 ] INFO App 程序已經啓動19 [ 2020-03-23 16:59:02 840 ] INFO App -----85程序已經啓動18 [ 2020-03-23 16:59:02 845 ] INFO App -----85程序已經啓動19