log4net是Apache log4j的.net版,是一套XML配置型的日志引擎,正好最近有个程序需要日志系统,为了整合方便,就决定研究一下。感谢郝伟推荐。
log4net系统划分了7个日志等级,如设置为"WARN",则DEBUG,INFO不会被记录。如下:
1. ALL
2. DEBUG
3. INFO
4. WARN
5. ERROR
6. FATAL
7. OFF
Creating the Sample Application
Step 1: 下载log4net
去logging.apache.org/log4net,来获取他们的最近版,在例子里我用的是incubating-log4net-1.2.9-beta.zip,期待正式版。
Step 2: 创建 ASP.NET Web 应用程序
创建这个就不多说了,在log4net-1.2.0-beta8\bin\net如图。找到对应的dll,添加到引用里。有的版本没有编译好的dll,那就自己编一下吧,用.NET V1.0的那个版本转换成1.1或2.0的。我觉得这不是工作失误,是故意的。他们玩java玩惯了的人好多都是这样。
Configuring the Sample Application
Step 3: 添加Assembly信息
打开AssemblyInfo.cs文件,添加:
[assembly: log4net.Config.DOMConfigurator()]
这句话的意思是log4net系统会自动寻找配置文件Web.config从而获得并加载其中的配置信息。如果想log4net随时监视配置文件以便重新加载的话就
稍微麻烦点了要这样写:
[assembly:log4net.Config.DOMConfigurator(ConfigFile="filename",ConfigFileExtension="log4net",Watch=true)]
Step 4: 添加配置信息
要编辑Web.config文件了,真是每次看见它都有惊喜啊。找到configuration标签,configSections要紧挨着写到它下面(一点都不替别人考虑,要在有一个也跟他一样霸道怎么办?那就单写个它的日志配置文件吧!我估计APACHE他们就是这么想的)。看看例子吧:
<?xml version="1.0" encoding="utf-8" ?>
![](/Images/OutliningIndicators/None.gif)
<configuration>
![](/Images/OutliningIndicators/None.gif)
<configSections>
![](/Images/OutliningIndicators/None.gif)
<section name="log4net"
![](/Images/OutliningIndicators/None.gif)
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net-net-1.0"
![](/Images/OutliningIndicators/None.gif)
/>
![](/Images/OutliningIndicators/None.gif)
</configSections>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
<!-- This section contains the log4net configuration settings -->
![](/Images/OutliningIndicators/None.gif)
<log4net>
![](/Images/OutliningIndicators/None.gif)
<!-- Define some output appenders -->
![](/Images/OutliningIndicators/None.gif)
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
![](/Images/OutliningIndicators/None.gif)
<layout type="log4net.Layout.PatternLayout">
![](/Images/OutliningIndicators/None.gif)
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />
![](/Images/OutliningIndicators/None.gif)
</layout>
![](/Images/OutliningIndicators/None.gif)
</appender>
![](/Images/OutliningIndicators/None.gif)
<!-- RollingFileAppender looks after rolling over files by size or date -->
![](/Images/OutliningIndicators/None.gif)
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
![](/Images/OutliningIndicators/None.gif)
<param name="File" value="C:\\log\\RollingLogHelloWorld.log" />
![](/Images/OutliningIndicators/None.gif)
<param name="AppendToFile" value="true" />
![](/Images/OutliningIndicators/None.gif)
<param name="MaxSizeRollBackups" value="10" />
![](/Images/OutliningIndicators/None.gif)
<param name="MaximumFileSize" value="1000" />
![](/Images/OutliningIndicators/None.gif)
<param name="RollingStyle" value="Size" />
![](/Images/OutliningIndicators/None.gif)
<param name="StaticLogFileName" value="true" />
![](/Images/OutliningIndicators/None.gif)
<layout type="log4net.Layout.PatternLayout">
![](/Images/OutliningIndicators/None.gif)
<param name="ConversionPattern" value="%d [%t] %-5p %-45c [%x] - %m%n" />
![](/Images/OutliningIndicators/None.gif)
</layout>
![](/Images/OutliningIndicators/None.gif)
</appender>
![](/Images/OutliningIndicators/None.gif)
<!-- FileAppender appends to a log and it is manually managed or size -->
![](/Images/OutliningIndicators/None.gif)
<appender name="FileAppender" type="log4net.Appender.FileAppender">
![](/Images/OutliningIndicators/None.gif)
<param name="File" value="LogHelloWorld.log" />
![](/Images/OutliningIndicators/None.gif)
<!-- Example using environment variables in params -->
![](/Images/OutliningIndicators/None.gif)
<!-- <param name="File" value="${TMP}\\ApplicationKit.log" /> -->
![](/Images/OutliningIndicators/None.gif)
<param name="AppendToFile" value="true" />
![](/Images/OutliningIndicators/None.gif)
<layout type="log4net.Layout.PatternLayout">
![](/Images/OutliningIndicators/None.gif)
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
![](/Images/OutliningIndicators/None.gif)
</layout>
![](/Images/OutliningIndicators/None.gif)
</appender>
![](/Images/OutliningIndicators/None.gif)
<!-- Setup the root category, add the appenders and set the default level -->
![](/Images/OutliningIndicators/None.gif)
<root>
![](/Images/OutliningIndicators/None.gif)
<level value="INFO" />
![](/Images/OutliningIndicators/None.gif)
<appender-ref ref="ConsoleAppender" />
![](/Images/OutliningIndicators/None.gif)
</root>
![](/Images/OutliningIndicators/None.gif)
<!-- ApplicationKit category - the presentation UI -->
![](/Images/OutliningIndicators/None.gif)
<logger name="WebForm1">
![](/Images/OutliningIndicators/None.gif)
<level value="INFO" />
![](/Images/OutliningIndicators/None.gif)
<appender-ref ref="FileAppender" />
![](/Images/OutliningIndicators/None.gif)
</logger>
![](/Images/OutliningIndicators/None.gif)
</log4net>
![](/Images/OutliningIndicators/None.gif)
</configuration>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
注意Logger节点的NAME属性设的是WebForm1,这里指的是类名。要给每一个需要添加LOG的类都添加一个这样的节点。想起了OSWORKFLOW工作流了,灵活与方便实用往往不可兼得。这里的级别是INFO,也就是说,INFO以下的不会被记录。
Running the Sample Application
Step 5: 添加一个 Log 类
首先是添加引用
using log4net;
using log4net.Config;
![](/Images/OutliningIndicators/None.gif)
//然后是实例化如果认真研究了配置文件,那么这个声明方式就很好理解了:
![](/Images/OutliningIndicators/None.gif)
private static readonly ILog log = LogManager.GetLogger("WebForm1");
![](/Images/OutliningIndicators/None.gif)
Step 6: 使用 Log对象
到了现在,所有的配置工作都已经完成了,接下来的工作就是测试一下,把他和应用层连起来。随便加一个按钮在响应事件Button1_Click event handler.中添加
log.Info("Hello World, I am a logger");
Step 7: Run the Application
现在可以运行了,应该是个这样子的:
这样每次点击按钮,在日至文件中就会出现
2006-02-13 10:12:30,671 [1228] INFO WebForm1 [] - Hello World, I am a logger这样一条记录。
总结:
简单易用,过一阵再研究SQL日志吧,DB才素王道,呵呵。
更多信息参见http://logging.apache.org/log4net/