C#在window服务配置Log4Net.dll

一、使用背景:

  C#window服务下添加一个日志记录程序集(Log4Net.dll)

二、添加和使用步骤如下:

下载并引入Log4Net.dll程序集到项目中

  下载地址:http://logging.apache.org/log4net/download_log4net.cgi

在App.Config中添加对应的节点



    
"log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

 在App.Config中添加Log4Net.dll初始化信息(主要一些按照什么格式存储,存储位置的配置)

  
  
  
  
  "ALL" />
  ref ref="SysAppender" />
  
  
  "WebLogger">
  "DEBUG" />
  
  "SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
  
  "File" value="ProJectsLogs/" />
  "AppendToFile" value="true" />
  "RollingStyle" value="Date" />
  "DatePattern" value=""Logs_"yyyyMMdd".txt"" />
  "StaticLogFileName" value="false" />
  "log4net.Layout.PatternLayout,log4net">
  "ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
  
  
  "consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
  "log4net.Layout.PatternLayout,log4net">
  "ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
  
  
  

 在AssemblyInfo.cs:配置文件中读取配置Log4net.dll

[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)]

 使用Log4Net.Dll记录日志

//首先实例化Log4net
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

//使用记录信息
log.Debug("Debug", new Exception("Debug"));
log.Info("Info", new Exception("Info"));
log.Warn("Warn", new Exception("Warn"));
log.Error("Error", new Exception("Error"));
log.Fatal("Fatal", new Exception("Fatal"));

 

你可能感兴趣的:(C#在window服务配置Log4Net.dll)