转自:http://journeyboy.blog.sohu.com/35181106.html
一个成熟的系统,绝不能缺少Log功能。
与其自己费劲设计,不如使用现成的。
对于.Net环境开发,开源的log4net就是不错的选择。
log4net是从java下的log工具 log4j发展过来的,顾名思义,log4net就是给.net用的。
log4net的官方简介如下:
log4net is a tool to help the programmer output log statements to a variety of output targets. log4net is a port of the excellent log4j framework to the .NET runtime. We have kept the framework similar in spirit to the original log4j while taking advantage of new features in the .NET runtime.
- Support for multiple frameworks
- Output to multiple logging targets
- Hierarchical logging architecture
- XML Configuration
- Dynamic Configuration
- Logging Context
- Proven architecture
- Modular and extensible design
- High performance with flexibility
log4net能够输出很多种类型的log信息,如下:
Description
log4net.Appender.AdoNetAppender |
Writes logging events to a database using either prepared statements or stored procedures. |
log4net.Appender.AnsiColorTerminalAppender |
Writes color highlighted logging events to a an ANSI terminal window. |
log4net.Appender.AspNetTraceAppender |
Writes logging events to the ASP trace context. These can then be rendered at the end of the ASP page or on the ASP trace page. |
log4net.Appender.ColoredConsoleAppender |
Writes color highlighted logging events to the application's Windows Console. |
log4net.Appender.ConsoleAppender |
Writes logging events to the application's Console. The events may go to either the standard our stream or the standard error stream. |
log4net.Appender.EventLogAppender |
Writes logging events to the Windows Event Log. |
log4net.Appender.FileAppender |
Writes logging events to a file in the file system. |
log4net.Appender.LocalSyslogAppender |
Writes logging events to the local syslog service (UNIX only). |
log4net.Appender.MemoryAppender |
Stores logging events in an in memory buffer. |
log4net.Appender.NetSendAppender |
Writes logging events to the Windows Messenger service. These messages are displayed in a dialog on a users terminal. |
log4net.Appender.OutputDebugStringAppender |
Writes logging events to the debugger. If the application has no debugger,the system debugger displays the string. If the application has no debugger and the system debugger is not active,the message is ignored. |
log4net.Appender.RemoteSyslogAppender |
Writes logging events to a remote syslog service using UDP networking. |
log4net.Appender.RemotingAppender |
Writes logging events to a remoting sink using .NET remoting. |
log4net.Appender.RollingFileAppender |
Writes logging events to a file in the file system. The RollingFileAppender can be configured to log to multiple files based upon date or file size constraints. |
log4net.Appender.SmtpAppender |
Sends logging events to an email address. |
log4net.Appender.TelnetAppender |
Clients connect via Telnet to receive logging events. |
log4net.Appender.TraceAppender |
Writes logging events to the .NET trace system. |
log4net.Appender.UdpAppender |
Sends logging events as connectionless UDP datagrams to a remote host or a multicast group using a UdpClient. |
废话不说,整个VB.Net的例子来看看怎么用。
1、 建立一个名为"TestLog4Net"的Windows Application工程
2、 添加对log4net.dll的引用
3、 打开"AssemblyInfo.vb"文件,在文件中添加下列代码:
<Assembly: log4net.Config.XmlConfiguratorAttribute(Watch:=True)>
这句话表明程序会监视配置文件的变化。一旦配置文件发生变化,相应的输出也会有所变化。
4、 打开"Form1.vb"文件,在文件开头添加下列代码:
Imports log4net
5、 打开"Form1.vb"文件,找到下列代码:
Public Class Form1
Inherits System.Windows.Forms.Form
然后在这个类之内找个地方添加下列代码
Private Shared ReadOnly Log As log4net.ILog = _
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
这行代码利用反射,可以自己设定当前上下文信息作为参数。
6、 在窗口上添加一个按钮"Button1",双击进入事件处理编写,如下:
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
With Log
.Debug("Button1_Click Debug")
.Error("Button1_Click Error")
.Fatal("Button1_Click Fatal")
.Info("Button1_Click Info")
End With
End Sub
7、 运行程序,可以看到什么Log文件也没有,嗯,这就对了。我的目的是让系统建立bin目录。
8 进入bin目录,创建文件"TestLog4Net.exe.config",内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Register a section handler for the log4net section -->
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>
<log4net>
<!-- the rollingFile Appender,which could save log to File,and according to the configuration,when the file reach 100kb,it will save the old file to the TestLog4Net.log.1,and the TestLog4Net.log.2 and that's all-->
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="TestLog4Net.log " />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type=" log4net.Layout.PatternLayout">
<conversionPattern value="%level %date %logger - %message %t%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
</configuration>
在此文件中,有如下配置:
<file value="TestLog4Net.log" />指明了输出的log文件名称为"TestLog4Net.log"。
<maximumFileSize value="100KB" />指明了输出的log文件最大长度为100KB。
<conversionPattern value="%level %date %logger - %message %t%n" />指明了输出内容的格式,下面会有展示。
<level value="DEBUG" />指明当前是DEBUG模式,如果改为<level value="RELEASE" />,则不会输出log内容。
9、 再次运行程序,点击Button1,在bin目录下出现log文件"TestLog4Net.net",内容如下:
DEBUG 2006-08-12 18:02:40,753 TestLog4Net.Form1 - Button1_Click Debug 2396
ERROR 2006-08-12 18:02:40,763 TestLog4Net.Form1 - Button1_Click Error 2396
FATAL 2006-08-12 18:02:40,763 TestLog4Net.Form1 - Button1_Click Fatal 2396
INFO 2006-08-12 18:02:40,763 TestLog4Net.Form1 - Button1_Click Info 2396
如果再点一次Button1,文件内容如下:
DEBUG 2006-08-12 18:02:40,763 TestLog4Net.Form1 - Button1_Click Info 2396
DEBUG 2006-08-12 18:03:53,508 TestLog4Net.Form1 - Button1_Click Debug 2396
ERROR 2006-08-12 18:03:53,508 TestLog4Net.Form1 - Button1_Click Error 2396
FATAL 2006-08-12 18:03:53,508 TestLog4Net.Form1 - Button1_Click Fatal 2396
INFO 2006-08-12 18:03:53,508 TestLog4Net.Form1 - Button1_Click Info 2396
10、例子到此为止。还有不明白?自己看log4net的FAQ吧。