Log4Net使用笔记
首先:Log4Net安装:只要从apache网站上下载一个log4net.dll文件文件,然后让程序引用即可。
其次:Log4Net配置:Log4Net的配置方式有两种,一种是放在应用程序的配置文件中,另一种是放在独立的文件中(如xml文件)
第一:放在应用程序的配置文件中
由于我是在Winform上使用的Log4Net,所以我就只说明Winform的配置文件如何配置,web的配置和winform差不多。在WinForm的配置文件中配置Log4Net,首先应该添加WinForm的引用配置文件,然后将Log4Net的配置代码写到配置文件中(默认配置文件的名称是"应用程序名称.exe.config"),配置代码写好后,打开AssemblyInfo.cs文件,在其中添加Log4Net配置的关联代码:
[assembly: log4net.Config.XmlConfiguratorAttribute(Watch = true)]
,现在可以再代码中使用Log4Net了。
第二:放在独立的配置文件中
新建一个"test.xml"文件,名称随意,将配置代码写入其中,然后放在应用程序的目录下的bin\bin\Debug目录下,打开AssemblyInfo.cs文件,在其中添加Log4Net配置的关联代码:
[assembly: log4net.Config.XmlConfiguratorAttribute(ConfigFile = "test.xml", Watch = true)] //程序集属性,指明了配置文件路径.
这样一个独立的Log4Net的配置文件就完成了。其实和写在配置文件中的步骤差不多!
下面就是Log4Net的配置代码了:
其实我也是刚使用,下面的代码是在网上找的,所以有什么不懂得就自己Google去吧,呵呵!当然,在这里要提一下代码的出处:
http://blog.csdn.net/zhoufoxcn/article/details/6029021 周公专栏
下面就上代码了:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> </configSections> <appSettings> <!-- To enable internal log4net logging specify the following appSettings key --> <add key="log4net.Internal.Debug" value="true"/> </appSettings> <log4net> <!--定义输出到文件中--> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <!--每条日志末尾的文字说明--> <footer value="by 周公" /> <!--定义文件存放位置--> <file value="LogFileAppender_log.txt" /> <appendToFile value="true" /> <datePattern value="yyyyMMdd-HH:mm:ss" /> <layout type="log4net.Layout.PatternLayout"> <!--每条日志末尾的文字说明--> <footer value="by 周公" /> <!--输出格式--> <!--样例:2010-11-17 15:50:23,443 [9] (D:/CSProjects/Log4NetDemo/Log4NetDemo/Program.cs:27) FATAL Log4NetDemo.Program [(null)] - fatal System.Exception: 在发生了一个致命错误,Exception Id:548828745--> <conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:文件:所在行%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" /> </layout> </appender> <!--定义输出到控制台命令行中--> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] (%file:%line) %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--使用Rolling方式记录日志 每个日志文件最大100KB,生成的日志文件名会是log.txt.1,log.txt.2 ...log.txt.10 如果记录的日志超过10个,会从log.txt.1开始覆盖 --> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="RollingFileAppender_log.txt" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="100KB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] (%file:%line) %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--使用Rolling方式记录日志 按照日来记录日志 --> <appender name="RollingLogFileAppender_DateFormat" type="log4net.Appender.RollingFileAppender"> <file value="RollingLogFileAppender_DateFormat_log.txt" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <!--<datePattern value="yyyyMMdd-HHmm" />--> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread](%file:%line) %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--记录到SQLite这样的单机数据库中去 创SQLite表的SQL语句: CREATE TABLE Log ( LogId INTEGER PRIMARY KEY, Date DATETIME NOT NULL, Level VARCHAR(50) NOT NULL, Logger VARCHAR(255) NOT NULL, Source VARCHAR(255) NOT NULL, Message TEXT DEFAULT NULL ); --> <appender name="AdoNetAppender_SQLite" type="log4net.Appender.AdoNetAppender"> <bufferSize value="100" /> <connectionType value="System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.66.0, Culture=neutral" /> <!--SQLite连接字符串--> <connectionString value="Data Source=c://log4net.db;Version=3;" /> <commandText value="INSERT INTO Log (Date, Level, Logger,Source, Message) VALUES (@Date, @Level, @Logger, @Source, @Message)" /> <parameter> <parameterName value="@Date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout" /> </parameter> <parameter> <parameterName value="@Level" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level" /> </layout> </parameter> <parameter> <parameterName value="@Logger" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%logger" /> </layout> </parameter> <parameter> <parameterName value="@Source" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%file:%line" /> </layout> </parameter> <parameter> <parameterName value="@Message" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter> </appender> <!--定义输出到SQL Server数据库中--> <!-- 在SQL Server中创建表的SQL语句 CREATE TABLE [dbo].[Log] ( [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 ); --> <appender name="AdoNetAppender_SQLServer" type="log4net.Appender.AdoNetAppender"> <bufferSize value="100" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data" /> <connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" /> <commandText value="INSERT INTO Log ([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> <!--定义输出到Oracle9i中--> <!-- 在Oracle9i中创建表的SQL语句 create table log ( Datetime timestamp(3), Thread varchar2(255), Log_Level varchar2(255), Logger varchar2(255), Message varchar2(4000) ); --> <appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender"> <connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient" /> <connectionString value="data source=[mydatabase];User ID=[user];Password=[password]" /> <commandText value="INSERT INTO Log (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" /> <bufferSize value="128" /> <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> </appender> <!--定义输出到IBM DB2中--> <!-- 在DB2中创建表的SQL语句 CREATE TABLE "myschema.LOG" ( "ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY ( START WITH +1 INCREMENT BY +1 MINVALUE +1 MAXVALUE +2147483647 NO CYCLE NO CACHE NO ORDER ), "DATE" TIMESTAMP NOT NULL, "THREAD" VARCHAR(255) NOT NULL, "LEVEL" VARCHAR(500) NOT NULL, "LOGGER" VARCHAR(255) NOT NULL, "MESSAGE" VARCHAR(4000) NOT NULL, "EXCEPTION" VARCHAR(2000) ) IN "LRGTABLES"; --> <appender name="AdoNetAppender_DB2" type="log4net.Appender.AdoNetAppender"> <bufferSize value="100" /> <connectionType value="IBM.Data.DB2.DB2Connection,IBM.Data.DB2" /> <connectionString value="server=192.168.0.0;database=dbuser;user Id=username;password=password;persist security info=true" /> <commandText value="INSERT INTO myschema.Log (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="500" /> <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> <!--定义输出到windows事件中--> <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--定义输出到数据库中,这里举例输出到Access数据库中,数据库为C盘的log4net.mdb 创建Access表的SQL语句: --> <appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender"> <connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/log4net.mdb" /> <commandText value="INSERT INTO LogDetails ([LogDate],[Thread],[Level],[Logger],[Message]) VALUES (@logDate, @thread, @logLevel, @logger,@message)" /> <!--定义各个参数--> <parameter> <parameterName value="@logDate" /> <dbType value="String" /> <size value="240" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date" /> </layout> </parameter> <parameter> <parameterName value="@thread" /> <dbType value="String" /> <size value="240" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%thread" /> </layout> </parameter> <parameter> <parameterName value="@logLevel" /> <dbType value="String" /> <size value="240" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level" /> </layout> </parameter> <parameter> <parameterName value="@logger" /> <dbType value="String" /> <size value="240" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%logger" /> </layout> </parameter> <parameter> <parameterName value="@message" /> <dbType value="String" /> <size value="240" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter> </appender> <!--定义日志的输出媒介,下面定义日志以四种方式输出。也可以下面的按照一种类型或其他类型输出。--> <root> <!--文件形式记录日志--> <appender-ref ref="LogFileAppender" /> <!--控制台控制显示日志--> <appender-ref ref="ConsoleAppender" /> <!--Windows事件日志--> <!--<appender-ref ref="EventLogAppender" />--> <!--SQLite日志--> <!--<appender-ref ref="AdoNetAppender_SQLite" />--> <!--RollingFileAppender事件日志--> <appender-ref ref="RollingFileAppender" /> <!--RollingFileAppender事件日志,每天一个日志--> <appender-ref ref="RollingLogFileAppender_DateFormat" /> <!-- 如果不启用相应的日志记录,可以通过这种方式注释掉 <appender-ref ref="AdoNetAppender_Access" /> --> </root> </log4net> <!--<system.diagnostics> <trace autoflush="true"> <listeners> <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="D:/CSProjects/Log4NetDemo/Log4NetDemo/bin/log4net.txt" /> </listeners> </trace> </system.diagnostics>--> </configuration>
最后在说一下,如何你采用xml文件来保存Log4Net的配置文件,如何在程序中使用Log4Net失败,很有可能是你的xml文件格式问题,所以用专门的xml工具打开,然后重新保存一下xml文件即可。