log4net是大家在日常開發(fā)中非常用的一個(gè)開源組件,下載地址:http://logging./log4net/download_log4net.cgi。 Appdender常用配置下面介紹的最常用的配置中的幾種: 1.ConoleAppender<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline"/> </layout> </appender> 2.FileAppender<appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="log.txt"/> <appendToFile value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date{yyyy/MM/dd HH:mm:ss} [current thread id:%thread] %-5level %logger [%property] - %message%newline"/> </layout> </appender> 3.RollingFileAppender這個(gè)是記錄系統(tǒng)日志中最常用的。 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net"> <file value="log/log" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <StaticLogFileName value="false"/> <CountDirection value="1"/> <datePattern value="yyyyMMdd""" /> <maxSizeRollBackups value="50" /> <maximumFileSize value="1MB" /> <Encoding value="UTF-8" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date{yyyy/MM/dd HH:mm:ss} [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> 4.AdoNetAppender使用SQLServer連接 <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <!--specified log table definition --> <![CDATA[ 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 ) ]]> <bufferSize value="100" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <connectionString value="Data Source=.;Initial Catalog=MyTest;User ID=sa;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> oracle數(shù)據(jù)庫中,使用ODP.NET連接:
<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender"> <!--specified log table definition --> <![CDATA[ create table log ( Datetime timestamp(3), Thread varchar2(255), Log_Level varchar2(255), Logger varchar2(255), Message varchar2(4000) ); ]]> <!--<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />--> <connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess, Version=10.2.0.100, Culture=neutral, PublicKeyToken=89b483f429c47342" /> <connectionString value="Data Source=databasename;User ID=username;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> 目前開發(fā)的系統(tǒng)中,我比較傾向于使用ODP.NET組件來訪問oracle,它要比微軟的提供OracleClient效率要高,而且操作很方便。 Logger配置<root> <level value="ERROR" /> <appender-ref ref="FileAppender" /> <appender-ref ref="ConsoleAppender" /> <appender-ref ref="RollingLogFileAppender"/> </root> <logger name="RollFileLog"> <level value="INFO"/> <appender-ref ref="RollingLogFileAppender"/> <appender-ref ref="ConsoleAppender" /> </logger> <logger name="SQLDBLog"> <level value="INFO"/> <appender-ref ref="AdoNetAppender"/> <appender-ref ref="ConsoleAppender" /> </logger> <logger name="Oracle10gLog"> <level value="INFO"/> <appender-ref ref="AdoNetAppender_Oracle"/> <appender-ref ref="ConsoleAppender" /> </logger> 需要注意的是,root節(jié)點(diǎn)中配置的level級別,可以被繼承到logger中。 使用log4net使用logtnet是,需要首先加載配置文件,然后在實(shí)例化需要制定使用的logger。 加載配置文件的方式一般使用XmlConfigurator.Configure來加載,在assembly.cs文件中加載可以最為方便,C/S、B/S系統(tǒng)均可以使用。 //[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"..\..\Config\Log4net.config", Watch = true)] [assembly: log4net.Config.XmlConfigurator(Watch = true)] 參數(shù)Watch=true表示日志組件監(jiān)視配置文件發(fā)生更改。 //log4net.Config.XmlConfigurator.Configure(); //log4net.ILog log = log4net.LogManager.GetLogger("RollFileLog"); //log4net.ILog log = log4net.LogManager.GetLogger("SQLDBLog"); //log4net.ILog log = log4net.LogManager.GetLogger("Oracle10gLog"); //XmlConfigurator.Configure(new System.IO.FileInfo(@"..\..\Config\Log4net.config")); ILog log = LogManager.GetLogger("MySQLDB"); log.Info("this is my log file test."); log.Error("this is my log file test->error."); log.Fatal("test"); |
|