VB.NET中使用Log4Net
首先,添加log4net.dll组件
另外,建立一个配置文件App.Config里
加入以下配置节
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="rollingFile" />
</root>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="True" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender>
<appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<logger name="Log4NetTest.LogTest">
<level value="DEBUG" />
<appender-ref ref="rollingFile" />
<appender-ref ref="SystemEvent" />
</logger>
</log4net>
level定义记录的日志级别,有以下几种
None
Fatal
ERROR
WARN
DEBUG
INFO
ALL
新建VB.NET项目,新建一Form
添加Log4Net声明
<Assembly: log4net.Config.XmlConfigurator()>
Public Class Form1Class Form1
Private logger As log4net.ILog = log4net.LogManager.GetLogger("Form1")
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
logger.Info("This is Log4Net Test Sample!")
logger.Debug("###########" & Me.TextBox1.Text)
MessageBox.Show("End")
End Sub
End Class
运行程序,在文本框输入数据后,点击按钮,在Debug目录下会生成一个log.txt文件
----------------------header--------------------------
2007-06-15 11:36:36,856 [10] INFO Form1 - This is Log4Net Test Sample!
2007-06-15 11:36:36,887 [10] DEBUG Form1 - ###########AAAAA
----------------------footer--------------------------
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/tyouvivi/archive/2007/06/15/1653346.aspx
原文链接:https://www.f2er.com/vb/262381.html