VB.Net如何使用log4net

VB.Net如何使用log4net,第1张

概述VB.Net如何使用log4net 2009-09-02 13:53 转自:http://journeyboy.blog.sohu.com/35181106.html 一个成熟的系统,绝不能缺少Log功能。 与其自己费劲设计,不如使用现成的。 对于.Net环境开发,开源的log4net就是不错的选择。 log4net是从java下的log工具 log4j发展过来的,顾名思义,log4net就是给. VB.Net如何使用log4net 2009-09-02 13:53

转自: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.

Feature列表如下: 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.
这么多的类型,我用的最多的还是RollingFileAppender 废话不说,整个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吧。

总结

以上是内存溢出为你收集整理的VB.Net如何使用log4net全部内容,希望文章能够帮你解决VB.Net如何使用log4net所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1285331.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存