其實在平時的開發過程中都是不怎么寫log的,覺得在debug中能看得一清二楚。同事小姐姐前輩,一直就我不寫log進行批判,但是我從來不改,哈哈。也算是遇到報應了,在最近一個工程里,本地調試一切正常,到了服務器上就不好使,百般無奈寫了人生第一個log,也記錄下來,方便以后查漏補缺,與大家共勉。
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
首先提供一個范本,着急用的童鞋可以直接下載,然后跳過2,直接看1和3。
Logger.cs: https://pan.baidu.com/s/1dZHxuQ
LoggerEnum.cs: https://pan.baidu.com/s/1i7gUEZr
1.創建文件:
按照以下文件目錄建好,也可以自行安排,無關緊要。
主要用到的就是Logger.cs,LoggerEnum.cs就是記錄log的type的枚舉。
2.填充內容:
根據自己的項目改一下namespace就好了
1)首先LoggerEnum.cs 類型枚舉,沒啥好講的
namespace ICUValidationService.Log { public enum LogType { All, Information, Debug, Success, Failure, Warning, Error } }
2)Logger.cs
using System; using System.IO; using System.Reflection; namespace ICUValidationService.Log { public class Logger { #region Instance private static object logLock; private static Logger _instance; private static string logFileName; private Logger() { } /// <summary> /// Logger instance /// </summary> public static Logger Instance { get { if (_instance == null) { _instance = new Logger(); logLock = new object(); logFileName = Guid.NewGuid() + ".log"; } return _instance; } } #endregion /// <summary> /// Write log to log file /// </summary> /// <param name="logContent">Log content</param> /// <param name="logType">Log type</param> public void WriteLog(string logContent, LogType logType = LogType.Information,string fileName =null) { try { string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); basePath = @"C:\APILogs"; if (!Directory.Exists(basePath + "\\Log")) { Directory.CreateDirectory(basePath + "\\Log"); } string dataString = DateTime.Now.ToString("yyyy-MM-dd"); if (!Directory.Exists(basePath + "\\Log\\" + dataString)) { Directory.CreateDirectory(basePath + "\\Log\\" + dataString); } string[] logText = new string[] { DateTime.Now.ToString("hh:mm:ss") + ": " + logType.ToString() + ": " + logContent }; if (!string.IsNullOrEmpty(fileName)) { fileName = fileName +"_"+ logFileName; } else { fileName = logFileName; } lock (logLock) { File.AppendAllLines(basePath + "\\Log\\" + dataString + "\\" + fileName, logText); } } catch (Exception) { } } /// <summary> /// Write exception to log file /// </summary> /// <param name="exception">Exception</param> public void WriteException(Exception exception,string specialText= null) { if (exception != null) { Type exceptionType = exception.GetType(); string text = string.Empty; if (!string.IsNullOrEmpty(specialText)) { text = text + specialText + Environment.NewLine; } text = "Exception: " + exceptionType.Name + Environment.NewLine; text += " " + "Message: " + exception.Message + Environment.NewLine; text += " " + "Source: " + exception.Source + Environment.NewLine; text += " " + "StackTrace: " + exception.StackTrace + Environment.NewLine; WriteLog(text, LogType.Error); } } } }
簡單而言,就兩個方法,一個寫簡單信息(WriteLog),一個寫error信息(WriteException)。WriteLog記錄log,WriteException調用WriteLog記錄log。
3.調用
這個是必須的,不然找不到
using ICUValidationService.Log;
try {
Logger.Instance.WriteLog("Start"); //do something
Logger.Instance.WriteLog("End");
} catch (Exception ex) { Logger.Instance.WriteException(ex); }
記錄的log如下:
當然除了特殊需求,或者調試,一般咱們只在catch中記錄就可以了