TinyFrame升级之六:全局日志的设计及实现

时间:2021-07-11 04:30:53

日志记录显然是框架设计中不可或缺的元素,在本框架中,我们将使用log4net作为日志记录的主体。下面来具体说明如何让框架继承log4net,并通过Autofac进行IOC注入。

首先,定义好我们的Log日志操作接口:

   1:  public interface ILoggerService
   2:      {
   3:          void Info(string message);
   4:          void Warn(string message);
   5:          void Debug(string message);
   6:          void Error(string message);
   7:          void Error(Exception ex);
   8:          void Fatal(string message);
   9:          void Fatal(Exception ex);
  10:      }

具体的解释我就不用说了,其中Info代表正常日志,Warn代表告警日志,Debug代表调试日志,Error代表错误日志,Fatal代表系统崩溃日志。

下面是其实现部分:

   1:  public class LoggerService:ILoggerService
   2:      {
   3:          public LoggerService()
   4:          {
   5:              log4net.Config.XmlConfigurator.Configure();
   6:              logger = LogManager.GetLogger(typeof(LoggerService));
   7:          }
   8:   
   9:          private readonly ILog logger;
  10:   
  11:          public void Info(string message)
  12:          {
  13:              logger.Info(message);
  14:          }
  15:          public void Warn(string message)
  16:          {
  17:              logger.Warn(message);
  18:          }
  19:          public void Debug(string message)
  20:          {
  21:              logger.Debug(message);
  22:          }
  23:          public void Error(string message)
  24:          {
  25:              logger.Error(message);
  26:          }
  27:          public void Error(Exception ex)
  28:          {
  29:              logger.Error(ex.Message, ex);
  30:          }
  31:          public void Fatal(string message)
  32:          {
  33:              logger.Fatal(message);
  34:          }
  35:          public void Fatal(Exception ex)
  36:          {
  37:              logger.Fatal(ex.Message, ex);
  38:          }
  39:      }

我在构造中对log4net进行了配置,同时获取了log4net的日志记录对象。我们的配置需要写到用户接口中的web.config文件中:

   1:  <configSections>
   2:      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
   3:  </configSections>
   4:   
   5:  <log4net>
   6:      <root>
   7:        <level value="DEBUG"/>
   8:        <appender-ref ref="LogFileAppender"/>
   9:      </root>
  10:      <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
  11:        <param name="File" value="C:\log.txt"/>
  12:        <param name="AppendToFile" value="true"/>
  13:        <rollingStyle value="Size"/>
  14:        <maxSizeRollBackups value="10"/>
  15:        <maximumFileSize value="10MB"/>
  16:        <staticLogFileName value="true"/>
  17:        <layout type="log4net.Layout.PatternLayout">
  18:          <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
  19:        </layout>
  20:      </appender>
  21:    </log4net>

之后在Aufofac中注入一下:

builder.RegisterType<LoggerService>().As<ILoggerService>().InstancePerHttpRequest();

最后,在页面中,我们可以通过构造函数获取其实例:

   1:  public BookService(IUnitOfWork unitOfWork
   2:              , IBook bookRepository
   3:              , IBookType bookTypeRepository
   4:              , IBookPlace bookPlaceRepository
   5:              , ICacheManager cacheManager
   6:              , ILoggerService logger
   7:              )
   8:          {
   9:              this.unitOfWork = unitOfWork;
  10:              this.bookRepository = bookRepository;
  11:              this.bookTypeRepository = bookTypeRepository;
  12:              this.bookPlaceRepository = bookPlaceRepository;
  13:              this.cacheManager = cacheManager;
  14:              this.logger = logger;
  15:          }
  16:   
  17:          private readonly IUnitOfWork unitOfWork;
  18:          private readonly IBook bookRepository;
  19:          private readonly IBookType bookTypeRepository;
  20:          private readonly IBookPlace bookPlaceRepository;
  21:          private readonly ICacheManager cacheManager;
  22:          private readonly ILoggerService logger;

得到的日志结果如下:

TinyFrame升级之六:全局日志的设计及实现