log4net使用

1:在需要使用log4net的项目引用log4net,这是肯定的

2:定义配置文件,web.config或者其它默认配置文件,自定义文件,下面示例采用的是自定义配置文件

<?xml version="1.0"?>
<configuration>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="ErrorFileAppender" />
<appender-ref ref="WarnFileAppender" />
<appender-ref ref="InfoFileAppender" />
<appender-ref ref="DebugFileAppender" />
</root>
<appender name="ErrorFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Log/log-error.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %p %c %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>

<appender name="WarnFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Log/log-warn.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %p %c %m%n" />
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<param name="LevelToMatch" value="WARN" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>

<appender name="InfoFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Log/log-info.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %p %c %m%n" />
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<param name="LevelToMatch" value="INFO" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>

<appender name="DebugFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Log/log-debug.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %p %c %m%n" />
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<param name="LevelToMatch" value="DEBUG" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
</log4net>
</configuration>

3:使用前关联配置文件有两种方式

3.1:在项目的AssemblyInfo.cs添加[assembly: log4net.Config.XmlConfigurator(ConfigFile = "App_Data/Log4.config", Watch = true)]

3.2:在Global.asax.cs的Application_Start中使用代码log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(Server.MapPath("~/App_Data/Log4.config")));

其中3.2方式可以在整个解决方案中使用,不需要再使用3.1

4:使用方式

ILog logger = LogManager.GetLogger("MyLogger");

logger.Warn("测试错误");

log4net使用,布布扣,bubuko.com

log4net使用

上一篇:PHP中,json汉字编码


下一篇:PHP学习之环境搭建