? C#亚博足球娱乐场注册--任意三数字加yabo.com直达官网之基于Log4Net本地日志服务简单实现 亚博足球娱乐场注册,亚博娱乐全天彩,亚博官网赢钱不给
VB.net 2010 视频亚博足球娱乐场注册--任意三数字加yabo.com直达官网 VB.net 2010 视频亚博足球娱乐场注册--任意三数字加yabo.com直达官网 VB.net 2010 视频亚博足球娱乐场注册--任意三数字加yabo.com直达官网
SQL Server 2008 视频亚博足球娱乐场注册--任意三数字加yabo.com直达官网 c#入门经典亚博足球娱乐场注册--任意三数字加yabo.com直达官网 Visual Basic从门到精通视频亚博足球娱乐场注册--任意三数字加yabo.com直达官网
  • C#亚博足球娱乐场注册--任意三数字加yabo.com直达官网之基于Log4Net本地日志服务简单实现

  • 2019-04-13 21:21 来源:未知

背景#

  1. 项目开发中,我们或多或少会使用诸如NLog,Log4Net,Kafka+ELK等等日志套件;

  2. 基于关注点分离原则,业务开发的时候不应该关注日志具体实现;并且后续能方便切换其他日志套件;

  3. 这里先实现基于文件的日志服务,在下一篇将实现基于Kafka+ELK;

  4. 具体源码:MasterChief

  5. Nuget:Install-Package MasterChief.DotNet.Core.Log

  6. 欢迎Star,欢迎Issues;

日志接口定义#


	
Copy
/// /// 日志记录接口 /// public interface ILogService { #region Methods /// /// Debug记录 /// /// 日志信息 void Debug(string message); /// /// Debug记录 /// /// 日志信息 /// 异常信息 void Debug(string message, Exception ex); /// /// Error记录 /// /// 日志信息 void Error(string message); /// /// Error记录 /// /// 日志信息 /// 异常信息 void Error(string message, Exception ex); /// /// Fatal记录 /// /// 日志信息 void Fatal(string message); /// /// Fatal记录 /// /// 日志信息 /// 异常信息 void Fatal(string message, Exception ex); /// /// Info记录 /// /// 日志信息 void Info(string message); /// /// Info记录 /// /// 日志信息 /// 异常信息 void Info(string message, Exception ex); /// /// Warn记录 /// /// 日志信息 void Warn(string message); /// /// Warn记录 /// /// 日志信息 /// 异常信息 void Warn(string message, Exception ex); #endregion Methods }

基于Log4Net本地文件日志服务实现#


	
Copy
/// /// 基于Log4Net的文件日志记录 /// public sealed class FileLogService : ILogService { #region Constructors static FileLogService() { DebugLogger = LogManager.GetLogger(DebugLoggerName); InfoLogger = LogManager.GetLogger(InfoLoggerName); WarnLogger = LogManager.GetLogger(WarnLoggerName); ErrorLogger = LogManager.GetLogger(ErrorLoggerName); FatalLogger = LogManager.GetLogger(FatalLoggerName); } #endregion Constructors #region Fields /// /// The debug logger name /// public const string DebugLoggerName = "DEBUG_FileLogger"; /// /// The error logger name /// public const string ErrorLoggerName = "ERROR_FileLogger"; /// /// The fatal logger name /// public const string FatalLoggerName = "FATAL_FileLogger"; /// /// The information logger name /// public const string InfoLoggerName = "INFO_FileLogger"; /// /// The warn logger name /// public const string WarnLoggerName = "WARN_FileLogger"; /// /// The debug logger /// public static readonly ILog DebugLogger; /// /// The error logger /// public static readonly ILog ErrorLogger; /// /// The fatal logger /// public static readonly ILog FatalLogger; /// /// The information logger /// public static readonly ILog InfoLogger; /// /// The warn logger /// public static readonly ILog WarnLogger; #endregion Fields #region Methods /// /// Debug记录 /// /// 日志信息 public void Debug(string message) { if (DebugLogger.IsDebugEnabled) DebugLogger.Debug(message); } /// /// Debug记录 /// /// 日志信息 /// 异常信息 public void Debug(string message, Exception ex) { if (DebugLogger.IsDebugEnabled) DebugLogger.Debug(message, ex); } /// /// Error记录 /// /// 日志信息 public void Error(string message) { if (ErrorLogger.IsErrorEnabled) ErrorLogger.Error(message); } /// /// Error记录 /// /// 日志信息 /// 异常信息 public void Error(string message, Exception ex) { if (ErrorLogger.IsErrorEnabled) ErrorLogger.Error(message, ex); } /// /// Fatal记录 /// /// 日志信息 public void Fatal(string message) { if (FatalLogger.IsFatalEnabled) FatalLogger.Fatal(message); } /// /// Fatal记录 /// /// 日志信息 /// 异常信息 public void Fatal(string message, Exception ex) { if (FatalLogger.IsFatalEnabled) FatalLogger.Fatal(message, ex); } /// /// Info记录 /// /// 日志信息 public void Info(string message) { if (InfoLogger.IsInfoEnabled) InfoLogger.Info(message); } /// /// Info记录 /// /// 日志信息 /// 异常信息 public void Info(string message, Exception ex) { if (InfoLogger.IsInfoEnabled) InfoLogger.Info(message, ex); } /// /// Warn记录 /// /// 日志信息 public void Warn(string message) { if (WarnLogger.IsWarnEnabled) WarnLogger.Warn(message); } /// /// Warn记录 /// /// 日志信息 /// 异常信息 public void Warn(string message, Exception ex) { if (WarnLogger.IsWarnEnabled) WarnLogger.Warn(message, ex); } #endregion Methods }

Log4Net 配置文件#


	
Copy
xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler" /> configSections> <log4net> <logger name="FATAL_FileLogger"> <level value="ALL" /> <appender-ref ref="FATAL_FileAppender" /> logger> <logger name="ERROR_FileLogger"> <level value="ALL" /> <appender-ref ref="ERROR_FileAppender" /> logger> <logger name="WARN_FileLogger"> <level value="ALL" /> <appender-ref ref="WARN_FileAppender" /> logger> <logger name="INFO_FileLogger"> <level value="ALL" /> <appender-ref ref="INFO_FileAppender" /> logger> <logger name="DEBUG_FileLogger"> <level value="ALL" /> <appender-ref ref="DEBUG_FileAppender" /> logger> <logger name="ConsoleLogger"> <level value="ALL" /> <appender-ref ref="ColoredConsoleAppender" /> logger> <appender name="FATAL_FileAppender" type="log4net.Appender.RollingFileAppender"> <file value=".\log\\FATAL\\" /> <appendToFile value="true" /> <maxSizeRollBackups value="5" /> <rollingStyle value="Date" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <maximumFileSize value="10MB" /> <datePattern value="yyyyMM\\yyyy-MM-dd".log"" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="---------------------------------------------------%newline发生时间:%date %newline事件级别:%-5level %newline事件来源:%logger%newline日志内容:%message%newline" /> layout> appender> <appender name="ERROR_FileAppender" type="log4net.Appender.RollingFileAppender"> <file value=".\log\\ERROR\\" /> <appendToFile value="true" /> <maxSizeRollBackups value="5" /> <rollingStyle value="Date" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <maximumFileSize value="10MB" /> <datePattern value="yyyyMM\\yyyy-MM-dd".log"" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="---------------------------------------------------%newline发生时间:%date %newline事件级别:%-5level %newline事件来源:%logger%newline日志内容:%message%newline" /> layout> appender> <appender name="WARN_FileAppender" type="log4net.Appender.RollingFileAppender"> <file value=".\log\\WARN\\" /> <appendToFile value="true" /> <maxSizeRollBackups value="5" /> <rollingStyle value="Date" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <maximumFileSize value="10MB" /> <datePattern value="yyyyMM\\yyyy-MM-dd".log"" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="---------------------------------------------------%newline发生时间:%date %newline事件级别:%-5level %newline事件来源:%logger%newline日志内容:%message%newline" /> layout> appender> <appender name="INFO_FileAppender" type="log4net.Appender.RollingFileAppender"> <file value=".\log\\INFO\\" /> <appendToFile value="true" /> <maxSizeRollBackups value="5" /> <rollingStyle value="Date" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <maximumFileSize value="10MB" /> <datePattern value="yyyyMM\\yyyy-MM-dd".log"" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="---------------------------------------------------%newline发生时间:%date %newline事件级别:%-5level %newline事件来源:%logger%newline日志内容:%message%newline" /> layout> appender> <appender name="DEBUG_FileAppender" type="log4net.Appender.RollingFileAppender"> <file value=".\log\\DEBUG\\" /> <appendToFile value="true" /> <maxSizeRollBackups value="5" /> <rollingStyle value="Date" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <maximumFileSize value="10MB" /> <datePattern value="yyyyMM\\yyyy-MM-dd".log"" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="---------------------------------------------------%newline发生时间:%date %newline事件级别:%-5level %newline事件来源:%logger%newline日志内容:%message%newline" /> layout> appender>