新建文件log4net.config
编辑文件log4net.config
<configuration>
<configSections>
<!--日志记录-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<root></root>
<logger name="Logger">
<level value="ALL" />
<appender-ref ref="ErrorRollingFileAppender" />
<appender-ref ref="InfoRollingFileAppender" />
</logger>
<!--数据日志-->
<appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="RuningLog\" />
<param name="AppendToFile" value="true" />
<param name="rollingStyle" value="Date" />
<param name="datePattern" value="yyyy-MM-dd.'Info.txt'" />
<param name="staticLogFileName" value="false"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d%n提示信息:%m%n线程:[%t] %5p%n类名:%c [%x]%n发生位置:%l%n%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<!--异常日志-->
<appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="ErrorLog\" />
<param name="AppendToFile" value="true" />
<param name="rollingStyle" value="Date" />
<param name="datePattern" value="yyyy-MM-dd.'Error.txt'" />
<param name="staticLogFileName" value="false"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d%n提示信息:%m%n线程:[%t] %5p%n类名:%c [%x]%n发生位置:%l%n%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
</log4net>
</configuration>
调用方法
private static readonly ILog infoLogger = LogManager.GetLogger("Logger");
infoLogger.Info("这里填提示信息");
需要在Assemblyinfo.cs中添加
[assembly: log4net.Config.XmlConfiguratorAttribute(ConfigFile = @"log4net.config", Watch = true)]
@"log4net.config"为刚才新建文件的路径
lo4net.config需要放在bin文件夹下