官方提供的下载中包含有详尽的说明文档。
http://logging.apache.org/log4net/download_log4net.cgi
需要提示的是如何在自己的WEB或者Winform项目中调用,以节约各位的时间。其实很简单,只要在:AssemblyInfo.cs中插入如下代码,即可成功调用
//log4net.config 是配置文件的名称,可以自行设置 //Watch 是表示在程序运行期间是否监视该配置文件的变动 [assembly: log4net.Config.XmlConfigurator( ConfigFile = "bin\\log4net.config", Watch = true )]
调用示例:
private static ILog log4User = LogManager.GetLogger( “logger-name” ); log4User.Info( "登录系统" );
配置文件示例
1、存入sqlserver
View Code
//log4net.config <?xml version="1.0" encoding="utf-8" ?> <log4net debug="false"> <appender name="AdoNetAppender_sqlserver" type="log4net.Appender.AdoNetAppender"> <bufferSize value="1" /> <connectionType value="System.Data.SqlClient.SqlConnection" /> <connectionString value="data source=localhost;initial catalog=KioskPisInfo;integrated security=false;persist security info=True;User ID=kiosk;Password=kiosk" /> <commandText value="INSERT INTO L4Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" /> <parameter> <parameterName value="@log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout" /> </parameter> <parameter> <parameterName value="@thread" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%thread" /> </layout> </parameter> <parameter> <parameterName value="@log_level" /> <dbType value="String" /> <size value="50" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level" /> </layout> </parameter> <parameter> <parameterName value="@logger" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%logger" /> </layout> </parameter> <parameter> <parameterName value="@message" /> <dbType value="String" /> <size value="4000" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter> <parameter> <parameterName value="@exception" /> <dbType value="String" /> <size value="2000" /> <layout type="log4net.Layout.ExceptionLayout" /> </parameter> </appender> <root> <level value="DEBUG" /> <appender-ref ref="AdoNetAppender_sqlserver" /> </root> </log4net>
<bufferSize value="1" />, bufferSize为缓存容量,当log4net缓存的行数超过该值,才会把数据行插入数据库。
数据库对应表脚本
View Code
CREATE TABLE [dbo].[L4Log] ( [Id] [int] IDENTITY (1, 1) NOT NULL, [Date] [datetime] NOT NULL, [Thread] [varchar] (255) NOT NULL, [Level] [varchar] (50) NOT NULL, [Logger] [varchar] (255) NOT NULL, [Message] [varchar] (4000) NOT NULL, [Exception] [varchar] (2000) NULL )
2、存入文本中
View Code
<?xml version="1.0" encoding="utf-8" ?> <log4net debug="false"> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="c:\kiosklog\ws.txt" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="600KB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="发生时间:%date%n事件级别:%-5level%n事件来源:%logger%n日志内容:%m%n------------------------------%n" /> </layout> </appender> <root> <level value="WARN" /> <appender-ref ref="RollingLogFileAppender" /> </root> </log4net>
|
请发表评论