log4net环境配置
引用 log4net.dll
添加log4net.config
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net debug="false">
<!--输出到文件-->
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<!--log文件存放的地址-->
<param name="File" value="d:\Applog.txt" />
<param name="datePattern" value="yyyy-MM-dd HH:mm" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<!--记录到系统日志-->
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<!--记录到分割的日志文件-->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="d:\log.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<!--指定用哪个方式记录-->
<appender-ref ref="FileAppender" />
<appender-ref ref="RollingLogFileAppender" />
</configSections>
<log4net debug="false">
<!--输出到文件-->
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<!--log文件存放的地址-->
<param name="File" value="d:\Applog.txt" />
<param name="datePattern" value="yyyy-MM-dd HH:mm" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<!--记录到系统日志-->
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<!--记录到分割的日志文件-->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="d:\log.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<!--指定用哪个方式记录-->
<appender-ref ref="FileAppender" />
<appender-ref ref="RollingLogFileAppender" />
<appender-refref="EventLogAppender
" />
</root>
</log4net>
</configuration>
添加Global.asax
在Application_Start 中添加如下:
log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
添加页面test.aspx
在test.aspx.cs 添加
private static readonly ILog log = LogManager.GetLogger(typeof(Login));
在PageLoad中添加
log.Debug("Test");
log.Error("Err");
log.Error("Err");
log.Info( "Info");
好了。配置完成