使用Log组件时,我们通常自定义ILogger接口,使用Log4Net等组件进行适配来定义不同的实现类。使用Log4Net日志组件时,为了即方便单元测试又能使用配置文件,我们通过Log4Net的ILogger接口使用Log4Net,对Log4Net进行单元测试时我们可以通过使用MemoryAppender方式测试。
1.定义ILogger接口
ILogger可以通过使用日志级别的枚举类型精简方法数量,也可以不适用枚举直接定义多个方法。
public interface ILogger { void Debug(string format, params string[] args); void Error(string format, params string[] args); void Fatal(string format, params string[] args); void Info(string format, params string[] args); void Warn(string format, params string[] args); }
2.创建使用Log4Net适配的实现类Log4NetAdapter
public class Log4NetAdapter : ILogger { private readonly ILog _rootLogger; public Log4NetAdapter(ILog rootLogger) { this._rootLogger = rootLogger; } public void Debug(string format, params string[] args) { if (this._rootLogger.IsDebugEnabled) { _rootLogger.Debug(string.Format(format, args)); } } public void Info(string format, params string[] args) { if (this._rootLogger.IsInfoEnabled) { _rootLogger.Info(string.Format(format, args)); } } public void Warn(string format, params string[] args) { if (this._rootLogger.IsWarnEnabled) { _rootLogger.Warn(string.Format(format, args)); } } public void Error(string format, params string[] args) { if (this._rootLogger.IsErrorEnabled) { _rootLogger.Error(string.Format(format, args)); } } public void Fatal(string format, params string[] args) { if (this._rootLogger.IsFatalEnabled) { _rootLogger.Fatal(string.Format(format, args)); } } }
3.使用MemoryAppender进行单元测试
public class Log4netAdapterTest { private MemoryAppender _appender; [Fact] public void Test() { _appender = new MemoryAppender { Name = "Unit Testing Appender", Layout = new log4net.Layout.PatternLayout("%message") }; _appender.ActivateOptions(); var root = ((Hierarchy)log4net.LogManager.GetRepository()).Root; root.AddAppender(_appender); root.Repository.Configured = true; ILogger logger = new Log4NetAdapter(log4net.LogManager.GetLogger("root")); logger.Info("test {0}", "message"); Assert.Equal(_appender.GetEvents()[0].MessageObject.ToString(), string.Format("test {0}", "message")); } }
4.创建Log4NetLoggerFactory
通常我们使用配置文件方式配置Log4Net,因此创建Log4NetLoggerFactory提供从配置文件中创建的ILog对象。
public class LoggerFactory { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(LoggerFactory)); static LoggerFactory() { log4net.Config.XmlConfigurator.Configure(); } public static ILogger GetLogger() { return new Log4NetAdapter(log); } }
参考的配置文件
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <log4net> <root> <level value="ALL" /> <appender-ref ref="RollingLogFileAppender" /> </root> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="App_Data/Logs/log.txt" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <preserveLogFileNameExtension value="true" /> <datePattern value="yyyyMMdd" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> </layout> </appender> </log4net>
运行结果:
4.通过依赖注入使用ILogger
在ASP.NET项目中使用时,通过DI容器进行注入。
protected void Application_Start() { //... ObjectFactory.AddSingleton<ILogger, Log4NetAdapter>(LoggerFactory.GetLogger()); //... }