相關資料:html
http://www.cnblogs.com/dragon/archive/2005/03/24/124254.htmlweb
注意:數據庫
//若是爲了使得應用程序的配置文件(web/app.config)不至於很臃腫,把log4net的配置單獨放置在
//Log4net.config文件中,那麼須要應用程序的AssemblyInfo.cs添加這行代碼。目的是程序啓動時是找到Log4net.config配置文件。windows
也能夠用代碼進行配置:架構
請在 應用程序啓動前調用:app
private void LoadLog4netConfig() { string filePath = Server.MapPath("~/Config/Log4net.config"); FileInfo fileInfo = new FileInfo(filePath); //DOMConfigurator已經被XmlConfigurator取代 //log4net.Config.DOMConfigurator.Configure(fileInfo); XmlConfigurator.ConfigureAndWatch(fileInfo); }
注意:框架
若是是分層架構中,假設LoadLog4netConfig是在程序集1中調用ide
string filePath = Server.MapPath("~/Config/Log4net.config");
仍是指的是調用了程序集1的應用程序的目錄
~/Config/Log4net.config
尋找,
故,配置文件Log4net.config應該添加在調用log4net的程序集中
一些代碼的用法示例:函數
protected void Button1_Click(object sender, EventArgs e) { //註釋掉其餘全部的Loger,只用<root>這個父loger //再把已經註釋掉其餘的Loger的<appender-ref ref="LogFileAppender" /> //複製到<root>,能夠同時進行全部子loger向的日誌方案 ILog rootLog = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); rootLog.Debug("I'm rootLoger"); //ILog logFileAppenderLoggingExample = LogManager.GetLogger("LogFileAppender.LoggingExample"); //ILog consoleAppLoggingExample = LogManager.GetLogger("ConsoleApp.LoggingExample"); //if (null != logFileAppenderLoggingExample) //{ // logFileAppenderLoggingExample.Debug("Hello, I'm LogFileAppender.LoggingExample, nothing, just say hello! "); // consoleAppLoggingExample.Debug("Hello, I'm consoleAppLoggingExample, nothing, just say hello! "); //} }
Log4net.config文件:spa
<?xml version="1.0" encoding="utf-8" ?> <!-- .NET application configuration file This file must have the exact same name as your application with .config appended to it. For example if your application is ConsoleApp.exe then the config file must be ConsoleApp.exe.config. It must also be in the same directory as the application. --> <configuration> <!-- Register a section handler for the log4net section --> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <appSettings> <!-- To enable internal log4net logging specify the following appSettings key --> <!-- <add key="log4net.Internal.Debug" value="true"/> --> </appSettings> <!-- This section contains the log4net configuration settings --> <log4net> <!-- Define some output appenders --> <!--Appender的組件來定義輸出介質--> <!--log4net.Appender.RollingFileAppender日誌文件超過maximumFileSize後自動建立第二個文件--> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="rolling-log.txt" /> <appendToFile value="true" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="100" /> <rollingStyle value="Size" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <header value="[Header] " /> <footer value="[Footer] " /> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> </layout> </appender> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <file value="log-file.txt" /> <!-- Example using environment variables in params --> <!-- <file value="${TMP}\log-file.txt" /> --> <sppendToFile value="true" /> <!-- An alternate output encoding can be specified --> <!-- <encoding value="unicodeFFFE" /> --> <layout type="log4net.Layout.PatternLayout"> <geader value="[Header] " /> <footer value="[Footer] " /> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" /> </layout> <!-- Alternate layout using XML <layout type="log4net.Layout.XMLLayout" /> --> </appender> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" /> </layout> </appender> <appender name="NetSendAppender" type="log4net.Appender.NetSendAppender"> <threshold value="ERROR" /> <server value="SQUARE" /> <recipient value="nicko" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> </layout> </appender> <!-- Example of how to configure the AdoNetAppender --> <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender" > <connectionString value="Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\Work\cvs_root\log4net-1.2\access.mdb;UID=;PWD=;" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES ('%date','%thread','%level','%logger','%message')" /> </layout> </appender> <appender name="A" type="log4net.Appender.ForwardingAppender" > <threshold value="WARN"/> <appender-ref ref="ConsoleAppender" /> </appender> <appender name="B" type="log4net.Appender.ForwardingAppender" > <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="DEBUG"/> <levelMax value="INFO"/> </filter> <appender-ref ref="ConsoleAppender" /> </appender> --> <!-- Setup the root category, add the appenders and set the default level --> <!-- 在框架的體系裏,全部的日誌對象都是根日誌(root logger)的後代。 所以若是一個日誌對象沒有在配置文件裏顯式定義,則框架使用根日誌中定義的屬性。 在<root>標籤裏,能夠定義level級別值和Appender的列表。若是沒有定義LEVEL的值, 則缺省爲DEBUG。能夠經過<appender-ref>標籤訂義日誌對象使用的Appender對象。 <appender-ref>聲明瞭在其餘地方定義的Appender對象的一個引用。 在一個logger對象中的設置會覆蓋根日誌的設置。 而對Appender屬性來講,子日誌對象則會繼承父日誌對象的Appender列表。 這種缺省的行爲方式也能夠經過顯式地設定<logger>標籤的additivity屬性爲false而改變。--> <root> <level value="All" /> <!--引用的appender的名稱--> <appender-ref ref="LogFileAppender" /> <appender-ref ref="ConsoleAppender" /> </root> <!-- Specify the level for some specific categories --> <!-- 元素預約義了一個具體日誌對象的設置。 而後經過調用LogManager.GetLogger(「testAPP.Logging」)函數, 你能夠檢索具備該名字的日誌。若是LogManager.GetLogger(…)打開的不是預約義的日誌對象, 則該日誌對象會繼承根日誌對象的屬性。知道了這一點,咱們能夠說, 其實<logger>標籤並非必須的。--> <!--<logger name="ConsoleApp.LoggingExample"> <level value="ALL" /> --><!--引用的appender的名稱--><!-- <appender-ref ref="ConsoleAppender" /> </logger> <logger name="LogFileAppender.LoggingExample"> <level value="ALL" /> --><!--引用的appender的名稱--><!-- <appender-ref ref="LogFileAppender" /> </logger>--> </log4net> </configuration>
官網代碼中的Lo4net配置文件:
<?xml version="1.0" encoding="utf-8" ?> <!-- .NET application configuration file This file must have the exact same name as your application with .config appended to it. For example if your application is ConsoleApp.exe then the config file must be ConsoleApp.exe.config. It must also be in the same directory as the application. --> <configuration> <!-- Register a section handler for the log4net section --> <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> <!-- This section contains the log4net configuration settings --> <log4net> <!-- Define some output appenders --> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="rolling-log.txt" /> <appendToFile value="true" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="100" /> <rollingStyle value="Size" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <header value="[Header] " /> <footer value="[Footer] " /> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> </layout> </appender> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <file value="log-file.txt" /> <!-- Example using environment variables in params --> <!-- <file value="${TMP}\log-file.txt" /> --> <sppendToFile value="true" /> <!-- An alternate output encoding can be specified --> <!-- <encoding value="unicodeFFFE" /> --> <layout type="log4net.Layout.PatternLayout"> <geader value="[Header] " /> <footer value="[Footer] " /> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" /> </layout> <!-- Alternate layout using XML <layout type="log4net.Layout.XMLLayout" /> --> </appender> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" /> </layout> </appender> <appender name="NetSendAppender" type="log4net.Appender.NetSendAppender"> <threshold value="ERROR" /> <server value="SQUARE" /> <recipient value="nicko" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> </layout> </appender> <!-- Example of how to configure the AdoNetAppender <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender" > <connectionString value="Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\Work\cvs_root\log4net-1.2\access.mdb;UID=;PWD=;" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES ('%date','%thread','%level','%logger','%message')" /> </layout> </appender> --> <!-- <appender name="A" type="log4net.Appender.ForwardingAppender" > <threshold value="WARN"/> <appender-ref ref="ConsoleAppender" /> </appender> <appender name="B" type="log4net.Appender.ForwardingAppender" > <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="DEBUG"/> <levelMax value="INFO"/> </filter> <appender-ref ref="ConsoleAppender" /> </appender> --> <!-- Example of how to configure the AdoNetAppender to connect to MS Access --> <appender name="ADONetAppender_Access" type="log4net.Appender.AdoNetAppender"> <connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Work\cvs_root\log4net-1.2\access.mdb;User Id=;Password=;" /> <commandText" value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" /> <parameter> <parameterName value="@log_date" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date" /> </param> </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="1024" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter> </appender> <!-- Example of how to configure the AdoNetAppender to connect to MS SQL Server --> <appender name="ADONetAppender_SqlServer" type="log4net.Appender.AdoNetAppender"> <bufferSize value="1" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <connectionString value="data source=SQLSVR;initial catalog=test_log4net;integrated security=false;persist security info=True;User ID=sa;Password=sa" /> <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" /> <parameter> <parameterName value="@log_date" /> <dbType value="DateTime" /> <!-- <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" /> </layout> --> <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> <!-- Setup the root category, add the appenders and set the default level --> <root> <level value="WARN" /> <appender-ref ref="LogFileAppender" /> <appender-ref ref="ConsoleAppender" /> <!-- <appender-ref ref="ADONetAppender_SqlServer" /> --> <appender-ref ref="NetSendAppender" /> <!-- <appender-ref ref="A" /> --> </root> <!-- Specify the level for some specific categories --> <logger name="ConsoleApp.LoggingExample"> <!-- <appender-ref ref="B" /> --> <level value="ALL" /> <appender-ref ref="RollingLogFileAppender" /> </logger> </log4net> </configuration>
另一個配置註釋:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=3.3.3.3001, Culture=neutral, PublicKeyToken=aa95f207798dfdb4" /> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <log4net> <!--定義輸出到文件中--> <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender,log4net"> <!--<appender name="LogFileAppender" type="log4net.Appender.FileAppender">--> <!--定義文件存放位置--> <file value="log4netfile.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd-HH:mm:ss" /> <layout type="log4net.Layout.PatternLayout"> <!--輸出格式--> <!--樣例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info--> <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] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </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--> <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" /> <!-- 若是不啓用相應的日誌記錄,能夠經過這種方式註釋掉 <appender-ref ref="AdoNetAppender_Access" /> --> </root> </log4net> </configuration>