开源测试_log4net

发布于:2024-11-29 ⋅ 阅读:(9) ⋅ 点赞:(0)

测试可用

代码

using log4net;
string logDirectory = "D:/log";
if (!Directory.Exists(logDirectory))
{
    Directory.CreateDirectory(logDirectory);
}

//初始化 log4net  
log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));

ILog errorLogger = LogManager.GetLogger("logerror");
ILog warningLogger = LogManager.GetLogger("logoperate");
ILog infoLogger = LogManager.GetLogger("loginfo");

//记录日志示例  
errorLogger.Error("This is an error message.");
warningLogger.Warn("This is a warning message.");
infoLogger.Info("This is an info message.");

try {
    int i = 0;
    var e = 1 / i;
}
catch(Exception e) {
    errorLogger.Error("error", e);
}

config

<configuration>
  <log4net>
    <!--错误日志类-->
    <logger name="logerror">
      <!--定义记录的日志级别-->
      <level value="ALL" />
      <appender-ref ref="ErrorAppender" />
      <appender-ref ref="InfoAppender" />
    </logger>
    <!--运行信息日志类-->
    <logger name="loginfo">
      <level value="ALL" />
      <appender-ref ref="InfoAppender" />
    </logger>
    <!--操作日志类-->
    <logger name="logoperate">
      <level value="ALL" />
      <appender-ref ref="OperateAppender" />
      <appender-ref ref="InfoAppender" />
    </logger>

    <!--错误日志附加介质-->
    <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
      <!-- name属性指定其名称,type则是log4net.Appender命名空间的一个类的名称,意思是,指定使用哪种介质-->
      <param name="File" value="D:\\Log\\Error" />
      <param name="AppendToFile" value="true" />
      <param name="MaximumFileSize" value="10MB" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name= "DatePattern" value= "yyyy-MM-dd&quot;.log&quot;"/>
      <param name="lockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="RollingStyle" value="Date" />
      <!--信息日志布局-->
      <layout type="log4net.Layout.PatternLayout">
        <!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />-->
        <param name="ConversionPattern" value="%date [%t] - %m%n" />
      </layout>
    </appender>

    <!--信息日志附加介质-->
    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="D:\\Log\\Info" />
      <param name="AppendToFile" value="true" />
      <param name="MaximumFileSize" value="10MB" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name= "DatePattern" value= "yyyy-MM-dd&quot;.log&quot;"/>
      <param name="lockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="RollingStyle" value="Date" />
      <!--信息日志布局-->
      <layout type="log4net.Layout.PatternLayout">
        <!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />-->
        <param name="ConversionPattern" value="%d [%t][%-1level] - %m%n" />
      </layout>
    </appender>


    <!--警告日志附加介质-->
    <appender name="OperateAppender" type="log4net.Appender.RollingFileAppender">
      <!-- name属性指定其名称,type则是log4net.Appender命名空间的一个类的名称,意思是,指定使用哪种介质-->
      <param name="File" value="D:\\Log\\Operate" />
      <param name="AppendToFile" value="true" />
      <param name="MaximumFileSize" value="10MB" />
      <!-- 保存天数-->
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name= "DatePattern" value= "yyyy-MM-dd&quot;.log&quot;"/>
      <param name="lockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="RollingStyle" value="Date" />
      <!--信息日志布局-->
      <layout type="log4net.Layout.PatternLayout">
        <!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />-->
        <param name="ConversionPattern" value="%d [%t] - %m%n" />
      </layout>
    </appender>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t]%-5p %c- %m%n" />
      </layout>
    </appender>
  </log4net>
</configuration>