C# log4net 配置

log4net.config配置文件:

C# log4net 配置
<?xml version="1.0" encoding="utf-8"?>
<log4net>
  <!-- 日志文件配置-->
  <root>
    <level value="ALL"/>
    <!--按文件存储日志-->
    <appender-ref ref="DebugAppender"/>
    <appender-ref ref="InfoAppender"/>
    <appender-ref ref="ErrorAppender" />
  </root>
  <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value=".\\Logs\\Error\\" />
    <!--日志记录的存在路-->
    <param name="AppendToFile" value="true" />
    <!--为true就表示日志会附加到文件,为false,则会重新创建一个新文件-->
    <param name="MaxSizeRollBackups" value="100" />
    <!--创建最大文件数-->
    <param name="maximumFileSize" value="10MB" />
    <!--文件大小-->
    <param name="StaticLogFileName" value="false" />
    <!--是否指定文件名-->
    <param name="DatePattern" value="yyyy-MM-dd&quot;.log&quot;"/>
    <!--文件格式-->
    <param name="RollingStyle" value="Composite" />
    <!--创建新文件的方式,可选为Size(按文件大小),Date(按日期),Once(每启动一次创建一个文件),Composite(按日期及文件大小),默认为Composite-->
    <layout type="log4net.Layout.PatternLayout">
      <!--输出内容布局-->
      <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      <!--method会影响性能-->
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter">
      <param name="LevelMin" value="ERROR" />
      <param name="LevelMax" value="ERROR" />
    </filter>
  </appender>
  <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value=".\\Logs\\Info\\" />
    <param name="AppendToFile" value="true" />
    <param name="MaxSizeRollBackups" value="100" />
    <param name="maximumFileSize" value="10MB" />
    <param name="StaticLogFileName" value="false" />
    <param name="DatePattern" value="yyyy-MM-dd&quot;.log&quot;" />
    <param name="RollingStyle" value="Composite" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter">
      <param name="LevelMin" value="INFO" />
      <param name="LevelMax" value="INFO" />
    </filter>
  </appender>
  <appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value=".\\Logs\\Debug\\" />
    <param name="AppendToFile" value="true" />
    <param name="MaxSizeRollBackups" value="100" />
    <param name="maximumFileSize" value="10MB" />
    <param name="StaticLogFileName" value="false" />
    <param name="DatePattern" value="yyyy-MM-dd&quot;.log&quot;" />
    <param name="RollingStyle" value="Composite" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter">
      <param name="LevelMin" value="DEBUG" />
      <param name="LevelMax" value="DEBUG" />
    </filter>
  </appender>
</log4net>
View Code

在代码中加载配置文件:

C# log4net 配置
    public partial class Form1 : Form
    {
        private log4net.ILog _log2 = null;

        public Form1()
        {
            InitializeComponent();

            //加载log4net配置文件
            UriBuilder uri = new UriBuilder(Assembly.GetExecutingAssembly().CodeBase);
            string path = Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));
            FileInfo configFile = new FileInfo(Path.Combine(path, "log4net.config"));
            log4net.Config.XmlConfigurator.Configure(configFile);

            _log2 = log4net.LogManager.GetLogger(typeof(Form1));
        }
View Code

 

上一篇:log4net SmtpAppender 配置


下一篇:log4net 写入数据库时需注意logger的additivity属性