/// <summary> /// 日誌處理幫助類 /// </summary> public class LogHelper { private static Queue<string> ErrorQueue = new Queue<string>(); static Action<string> ...
/// <summary> /// 日誌處理幫助類 /// </summary> public class LogHelper { private static Queue<string> ErrorQueue = new Queue<string>(); static Action<string> errorAction; static string LogPath => System.Web.Hosting.HostingEnvironment.MapPath($"/App_Data/Log/{DateTime.Now.ToString("yyyyMMdd")}.txt"); static LogHelper() { errorAction += WriteFile; Task.Run(() => { while (true) { if (ErrorQueue.Count > 0) { lock (ErrorQueue) { var txt = ErrorQueue.Dequeue(); //出隊列 errorAction?.Invoke(txt); //寫日誌 } } else { Task.Delay(100); } } }); } /// <summary> /// 講內容寫入日誌隊列中 /// </summary> /// <param name="errorText"></param> public static void WriteLog(string errorText) { lock (ErrorQueue) { ErrorQueue.Enqueue(errorText); //進隊列 } } /// <summary> /// 寫入文本文件 /// </summary> /// <param name="txt"></param> static void WriteFile(string txt) { using (System.IO.FileStream fs = new System.IO.FileStream(LogPath, System.IO.FileMode.Append, System.IO.FileAccess.Write)) { var byteStr = Encoding.Default.GetBytes(txt); fs.Write(byteStr, 0, byteStr.Length); } } }View Code
Asp.Net Mvc日誌處理幫助類
看到公司框架日誌處理類寫得~~~哎,不想說了。nuget裝了log4Net然而都不用。。。只能改造一下湊合用了