<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<!--日志-->
<log4net>
<logger name="logerror">
<level value="ERROR" />
<appender-ref ref="ErrorAppender" />
</logger>
<logger name="loginfo">
<level value="INFO" />
<appender-ref ref="InfoAppender" />
</logger>
<logger name="logconsole">
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</logger>
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Logs\ErrorLog.log" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaximumFileSize" value="1MB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p %d [%c] %m%n" />
</layout>
</appender>
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Logs\InfoLog.log" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaximumFileSize" value="1MB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p %d [%c] %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %m%n" />
</layout>
</appender>
</log4net>
</configuration>
log4net.config
我创建的是一个web工程
方式1:找到Properties/AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
然后在web.config内增加节点,
<appSettings>
<add key="log4net.Config" value="log4net.config"/>
<add key="log4net.Config.Watch" value="True"/>
</appSettings>
输出日志的代码:
log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");//对应log4net.config内的节点
loginfo.Info("test test test");
方法2:
方式2:无需上面的配置:
在Global.asax的Application_Start初始化
var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
XmlConfigurator.ConfigureAndWatch(logCfg);//监听配置文件的变化
然后在其他地方就可以写日志了。
log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
loginfo.Info("test test test********************************");