LogHelper.cs
using NLog; using NLog.Targets; namespace MyProject.Tool.Log { public class LogHelper { readonly FileTarget _target; readonly Logger _logger; static LogHelper _instance; private static readonly object m_syncRoot = new object(); public static LogHelper GetInstance() { if (_instance == null) { lock (m_syncRoot) { if (_instance == null) { _instance = new LogHelper(); } } } return _instance; } private LogHelper() { //初始化 NLog 的一些信息 _target = new FileTarget(); _target.Layout = "${longdate} ${logger} ${message}"; _target.FileName = "${basedir}/log/${shortdate}.log"; _target.KeepFileOpen = false; NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(_target, LogLevel.Debug); _logger = LogManager.GetLogger("TeMai:"); } /// <summary> /// 写日志信息 /// </summary> /// <param name="message">具体的消息内容</param> public void WriteMessage(string message) { _logger.Debug(message); } } }
LogHelper.GetInstance().WriteMessage("UserInfoClient---UserInfoLogin:" + ex.Message);
日志插件对比
https://dotnet.libhunt.com/project/log4net/vs/semantic-logging?rel=cmp-cmp
日志记录插件源码:
https://github.com/NLog/NLog
https://github.com/serilog/serilog
https://github.com/apache/log4net
https://github.com/elmah/Elmah
https://github.com/logary/logary