VB.Net如何使用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.AdoNetAppenderWrites logging events to a database using either prepared statements or stored procedures.
log4net.Appender.AnsiColorTerminalAppenderWrites color highlighted logging events to a an ANSI terminal window.
log4net.Appender.AspNetTraceAppenderWrites 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.ColoredConsoleAppenderWrites color highlighted logging events to the application's Windows Console.
log4net.Appender.ConsoleAppenderWrites logging events to the application's Console. The events may go to either the standard our stream or the standard error stream.
log4net.Appender.EventLogAppenderWrites logging events to the Windows Event Log.
log4net.Appender.FileAppenderWrites logging events to a file in the file system.
log4net.Appender.LocalSyslogAppenderWrites logging events to the local syslog service (UNIX only).
log4net.Appender.MemoryAppenderStores logging events in an in memory buffer.
log4net.Appender.NetSendAppenderWrites logging events to the Windows Messenger service. These messages are displayed in a dialog on a users terminal.
log4net.Appender.OutputDebugStringAppenderWrites 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.RemoteSyslogAppenderWrites logging events to a remote syslog service using UDP networking.
log4net.Appender.RemotingAppenderWrites logging events to a remoting sink using .NET remoting.
log4net.Appender.RollingFileAppenderWrites 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.SmtpAppenderSends logging events to an email address.
log4net.Appender.TelnetAppenderClients connect via Telnet to receive logging events.
log4net.Appender.TraceAppenderWrites logging events to the .NET trace system.
log4net.Appender.UdpAppenderSends 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,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
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吧。


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ExtJs简介: Extjs在经过两年的发展,Ext JS从2.0版开始,越来越受用户欢迎,今年,Extjs不但推出3.0版本,而且还推出了Ext Core,准备在Web2.0网站开发中占一席之地,如在 Extjs2.x版本中为人所诟病的速度问题在ExtJs3.0中有所改善。不过,最革命性的改变还是ExtJs中新增的Ext.Direct功能,它实现了服务器端的无关性。 在3.2版本中,Ext将增加移动组件,进军移动市场,这将是一次革命性的改进,同时在4.0版本中,除了对HTML5的支持外,还增加画布功能。 还有一点更值得期待,就是Ext的RAD开发工具也在开发当中。估计在不久之后,也可以向VB,C#一样,通过可视化工具拖拽方式即可轻松开发Web应用。 ExtJs在发展过程中不仅一步步地巩固着自己在HTML、CSS、JavaScript领域无可比拟的优势,而且已经开始向相关领域发展扩张。如从2.02版开始为Adobe的RIA技术AIR提供支持,并且为GWT开发了Ext GWT2.0,这些都体现了ExtJs的强大活力和生命力。 在可预见的未来,ExtJs将会甩开对手,大踏步向前。 ExtJs的前景: ExtJS的前景是非常好的,现在的QQ2009的登录界面以及使用,迅雷最新版的主界面,都能够找到这个框架的踪迹。web QQ也是有一个技术版本是使用这种框架的,所以,可以看出,extjs的应用,是越来越广泛了,extjs的前景,不可估量,不论站在技术开发的层次还是大部分用户的使用体验。 国讯教育通用智能OA办公系统项目培训目标 本系列讲座分为四大部分: 1、ExtJs基础篇:主要介绍Ext Core的一些核心功能 2、ExtJs进阶篇:主要介绍ExtJs里的常用组件,容器及布局 3、ExtJs数据篇:主要介绍和数据库交互的相关组件及CRUD功能 4、项目实战篇: Extjs3.2+ASP.NET七层架构+设计模式+ log4j+WebSerice等技术国讯教育通用智能OA办公平台 适用对象 1、要求有一定的javascript语言和HTML,CSS基础的学员 2、有一定的Asp.net网页编程基础和C#语言基础 3、有志于从事富客户端技术ExtJs的学习与研究的学生及专业Web开发人员 模块介绍 1、ExtJs基础篇-ExtJs快速入门 1.1、ExtJs基础篇(1):ExtJs概述与环境配置及架构剖析 1.2、ExtJs基础篇(2):ExtJs OOP基础 1.3、ExtJs基础篇(3):ExtJs 核心函数简介 1.4、ExtJs基础篇(4):ExtJs中的模板详解(1) 1.5、ExtJs基础篇(5):ExtJs中的模板详解(2) 2、ExtJs进阶篇:Extjs进阶 2.1、大话ExtJs中的布局 2.2、ExtJs的常见组件 2.3、ExtJs中的面板及Window窗口 2.4、ExtJs中的选项卡面板 2.5、ExtJs中的对话框与Combox组件 2.6、ExtJs中的ExtTree详解 3、ExtJs数据篇 3.1、数据存储基本单元Record与DataField详解 3.2、数据存储Store详解1 3.3、数据存储Store详解2 3.4、数据代理DataProxy详解 3.5、数据读取器DataReader详解 3.6、Ext.Direct详解1 3.7、Ext.Direct详解2 4、ExtJs实战篇—国讯教育通用智能OA办公平台 (共70讲) 4.1、系统业务流程主功能结构分析 4.2、数据库设计 4.3、抽象工厂+反射七层架构设计 4.4、首页布局设计 4.5、人事管理模块分析设计 4.6、个人专区模块分析设计 4.7、日程管理区模块分析设计 4.8、文档管理模块分析设计 4.9、工单管理模块分析设计 4.10、工资管理模块分析设计 4.11、内部邮箱模块分析设计 4.12、系统管理模块分析设计 4.13、考勤管理模块分析设计 4.14、消息管理模块分析设计 4.15、日志管理 4.16、报表打印及数据统计 4.17、数据导入导出管理 本项目所涉及到的技术: 数据库方面: 1、PD数据库建模 2、SQL Server2005视图、存储过程、用户自定义函数、触发器 ASP.net方面: 1、ASP.net PetShop七层架构 2、抽象工厂+反射+配置文件实现数据库无缝切换 3、序列化/反序列化+泛型集合的应用 4、利用ASP.net HttpHandler实现防盗链 5、网站安全性方面:ASP.net防SQL注入及Web Service Soap头加密技术 6、AS

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值