其实在平时的开发过程中都是不怎么写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中记录就可以了