說明自從上次在2008年在博客上發表過有關log4net的用法介紹文章以後(網址:http://blog.csdn.net/zhoufoxcn/archive/2008/03/26/2220533.aspx),有很多朋友在博文下留言詢問一些細節,如今就一些比較廣泛的問題作一些稍微深刻的解答,但願你們滿意。web
- <configSections>
- <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
- </configSections>
- <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>
- create table log (
- Datetime timestamp(3),
- Thread varchar2(255),
- Log_Level varchar2(255),
- Logger varchar2(255),
- Message varchar2(4000)
- );
在本例中周公採用了將日誌記錄到SQLite這個單機數據庫的方式,而且還將記錄記錄日誌時的文件名和行號,建立SQLite的SQL語句以下: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>節點配置以下:數據庫
- <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>
- <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>
- <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>
- <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>
- using System;
- using System.Collections.Generic;
- using System.Text;
- using log4net;
- using System.Reflection;
- //注意下面的語句必定要加上,指定log4net使用.config文件來讀取配置信息
- //若是是WinForm(假定程序爲MyDemo.exe,則須要一個MyDemo.exe.config文件)
- //若是是WebForm,則從web.config中讀取相關信息
- [assembly: log4net.Config.XmlConfigurator(Watch = true)]
- namespace Log4NetDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- Random random = new Random();
- for (int i = 0; i < 1; i++)
- {
- //建立日誌記錄組件實例
- //ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- ILog log=log4net.LogManager.GetLogger(typeof(Program));
- //記錄錯誤日誌
- //log.Error("error", new Exception("在這裏發生了一個異常,Error Number:"+random.Next()));
- //記錄嚴重錯誤
- //log.Fatal("fatal", new Exception("在發生了一個致命錯誤,Exception Id:"+random.Next()));
- //記錄通常信息
- //log.Info("提示:系統正在運行");
- //記錄調試信息
- //log.Debug("調試信息:debug");
- //記錄警告信息
- //log.Warn("警告:warn");
- }
- Console.WriteLine("日誌記錄完畢。");
- Console.Read();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using log4net;
- using System.Reflection;
- [assembly: log4net.Config.XmlConfigurator(Watch = true)]
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- Random random = new Random();
- for (int i = 0; i < 1; i++)
- {
- //建立日誌記錄組件實例
- ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- //ILog log = log4net.LogManager.GetLogger(typeof(Program));
- //記錄錯誤日誌
- //log.Error("error", new Exception("在這裏發生了一個異常,Error Number:"+random.Next()));
- //記錄嚴重錯誤
- //log.Fatal("fatal", new Exception("在發生了一個致命錯誤,Exception Id:"+random.Next()));
- //記錄通常信息
- //log.Info("提示:系統正在運行");
- //記錄調試信息
- //log.Debug("調試信息:debug");
- //記錄警告信息
- log.Warn("警告:warn");
- Response.Write("日誌記錄完畢。");
- }
- }
- }
- }
能夠看出它們的代碼基本沒有區別。apache
下面是一個在WinForm下的config文件的完整配置,該配置文件所使用的代碼就是在上面所使用到的代碼,使用LogFileAppender、ConsoleAppender、EventLogAppender、AdoNetAppender_SQLite、RollingFileAppender、RollingLogFileAppender_DateFormat方式記錄日誌都在本地經過測試。完整的config請見附件。緩存
總結:網絡
本篇主要是補充在上一篇關於Log4Net中未盡之處,並集中詳盡回答了一些朋友在該篇博文下的提問,若有未盡之處,請在本篇下留言。若有初學者碰巧路過不知config文件爲什麼物,請看《asp.net夜話之十一:web.config詳解》,網址是:http://zhoufoxcn.blog.51cto.com/792419/166441。app
周公
2010-11-23框架