Log4.net示例
阅读原文时间:2023年07月09日阅读:3

//先用Nuget安装最新Log4net
using System;
using System.IO;
using log4net;
using log4net.Config;
using log4net.Repository;

namespace ConsoleApp
{
public class Program
{
static void Main(string[] args) {

        ILoggerRepository repository = LogManager.CreateRepository("repository");

        //默认简单配置,输出到控制台  
        //BasicConfigurator.Configure(repository);  
        XmlConfigurator.Configure(repository, new FileInfo("log.config"));

        ILog log = LogManager.GetLogger(repository.Name, "log");  
        log.Info("info");  
        log.Error("error");

        Console.ReadLine();  
    }  
}

}

log.config配置文件




<appender name="FileAppender" type="log4net.Appender.FileAppender">  
  <file value="log-file.log" />  
  <appendToFile value="true" />  
  <layout type="log4net.Layout.PatternLayout">  
    <conversionPattern value="%date \[%thread\] %-5level %logger \[%property{NDC}\] - %message%newline" />  
  </layout>  
</appender>

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">  
  <file value="logfile/" />  
  <appendToFile value="true" />  
  <rollingStyle value="Composite" />  
  <staticLogFileName value="false" />  
  <datePattern value="yyyyMMdd'.log'" />  
  <maxSizeRollBackups value="10" />  
  <maximumFileSize value="1MB" />  
  <layout type="log4net.Layout.PatternLayout">  
    <conversionPattern value="%date \[%thread\] %-5level %logger \[%property{NDC}\] - %message%newline" />  
  </layout>  
</appender>

<!-- Setup the root category, add the appenders and set the default level -->  
<root>  
  <level value="ALL" />  
  <appender-ref ref="ConsoleAppender" />  
  <appender-ref ref="FileAppender" />  
  <appender-ref ref="RollingLogFileAppender" />  
</root>


引用地址 https://www.cnblogs.com/linezero/p/log4net.html

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器