C# 中使用log4net 日志记录

1 首先在nuGet管理器上下载Log4Net包
2 在项目的根目录下新建log4net.config 文件
3 配置log4net,具体详细配置如下:


<configuration>
  <configSections>
    
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  configSections>

  <log4net>
    
    <root>
      
      
      <level value="ALL"/>
      
      <appender-ref ref="DEBUG_FILE"/>
      <appender-ref ref="INFO_FILE"/>
      <appender-ref ref="ERROR_FILE"/>
      
      
    root>

    <appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" >
      <sink value="tcp://localhost:8085/LoggingSink" />
      <lossy value="false" />
      <bufferSize value="95" />
      <onlyFixPartialEventData value="true" />
    appender>

    
    <appender  name="DEBUG_FILE" type="log4net.Appender.RollingFileAppender,log4net" >
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="DEBUG" />
      filter>

      
      <file value="log/app-log/" />
      
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      
      <DatePattern value="\debu\g-yyyy-MM-dd.lo\g">DatePattern>
      
      <param name="Encoding" value="utf-8"/>
      
      <param name="MaxSizeRollBackups" value="100"/>
      
      <param name="MaximumFileSize" value="500KB"/>

      
      <param name="StaticLogFileName" value="false"/>
      
      
      
      <param name="lockingModel"  type="log4net.Appender.FileAppender+MinimalLock" />

      <layout type="log4net.Layout.PatternLayout">
        
        
        <conversionPattern value="%date [%thread] %-5level %logger %method(%line) [%property{NDC}] %message %exception %newline"/>
      layout>

      
    appender>

    
    <appender  name="INFO_FILE" type="log4net.Appender.RollingFileAppender,log4net" >

      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO" />
        <param name="LevelMax" value="INFO" />
      filter>

      
      <file value="log/app-log/" />
      
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      
      <DatePattern value="in\fo-yyyy-MM-dd.lo\g">DatePattern>
      
      <param name="Encoding" value="utf-8"/>
      
      <param name="MaxSizeRollBackups" value="100"/>
      
      <param name="MaximumFileSize" value="500KB"/>
      
      <param name="StaticLogFileName" value="false"/>
      
      
      
      <param name="lockingModel"  type="log4net.Appender.FileAppender+MinimalLock" />

      <layout type="log4net.Layout.PatternLayout">
        
        <conversionPattern value="%date [%thread] %-5level %logger %method(%line) [%property{NDC}] %message %exception %newline"/>
      layout>

    appender>

    
    <appender  name="ERROR_FILE" type="log4net.Appender.RollingFileAppender,log4net" >

      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="ERROR" />
        <param name="LevelMax" value="ERROR" />
      filter>



      
      <file value="log/app-log/" />
      
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      
      <DatePattern value="error-yyyy-MM-dd.lo\g">DatePattern>
      
      <param name="Encoding" value="utf-8"/>
      
      <param name="MaxSizeRollBackups" value="100"/>
      
      <param name="MaximumFileSize" value="500KB"/>
      
      <param name="StaticLogFileName" value="false"/>
      
      <param name="lockingModel"  type="log4net.Appender.FileAppender+MinimalLock" />

      <layout type="log4net.Layout.PatternLayout">
        
        <conversionPattern value="%date %-5level %logger %method(%line) [%property{NDC}] %message %exception %newline"/>
      layout>
    appender>

    
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      

      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger %method(%line) [%property{NDC}] %message%newline"/>
      layout>
    appender>

    
  log4net>
  configuration>

4 在AssemblyInfo.cs 类中添加配置文件引用:

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

5 测试配置是否成功:

using log4net;
using Survey;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {

        private static readonly ILog logger = LogManager.GetLogger(typeof(Program));
        static void Main(string[] args)
        {
            logger.Info("info");
            logger.Error("error");
            logger.Debug("debug");


        }
    }
}

最后会在xxx/ConsoleApp1\bin\Debug\log/app-log/下生成三个日志文件的话就成功了!

关于log4Net更多详细信息请参考:
log4Net使用及配置注解(整理)
非常完善的Log4net详细说明
log4Net官方例子

你可能感兴趣的:(c#,系统日志)