log4net 將日誌寫入數據庫

 

      asp.net利用log4net寫入日誌到SqlServer數據庫,Log4net是一個開源的錯誤日誌記錄項目,易用性強,源自log4j,品質值得信賴。 下面就個人安裝部署log4net到MS sql server的經驗與你們分享。web

  下載log4net的Dll文件並引入到項目中,在你的web主項目(好比MainWeb.csproj)添加對log4net.dll的引用,並在相應的Globall.cs文件中的Application_Start 事件中寫入sql

     

           protected   void  Application_Start(Object sender, EventArgs e)  
        { 
log4net.Config.XmlConfigurator.Configure( new System.IO.FileInfo( "log4net.config" )); }

在這個項目對應的Assembly.cs文件中添加一行數據庫

 

[assembly: log4net.Config.XmlConfigurator(ConfigFile =  "log4net.config" , Watch =  true )]  

 

添加logn4net.confgi在主項目根目錄下:與web.config同路徑,之因此不寫入web,config文件, 是由於這樣能夠單獨修改調試部署,也就是非侵入式安裝的思想。app

 

<?xml version="1.0"?>  
<configuration>  
    <configSections>  
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />  
    </configSections>  
    <!--Log4net Begin by Tony  2008.11.20--> 
    <log4net>  
        <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">  
            <bufferSize value="10" />  
            <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />  
            <connectionString value="DATABASE=DBErrorLogs;SERVER=myserver;UID=user;PWD=pwdpwd;Connect Timeout=15;" />  
            <commandText value="INSERT INTO ErrorLog ([dtDate],[sThread],[sLevel],[sLogger],[sMessage],[sException]) 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="100" />  
                <layout type="log4net.Layout.PatternLayout">  
                    <conversionPattern value="%t" />  
                </layout>  
            </parameter>  
            <parameter>  
                <parameterName value="@log_level" />  
                <dbType value="String" />  
                <size value="200" />  
                <layout type="log4net.Layout.PatternLayout">  
                    <conversionPattern value="%p" />  
                </layout>  
            </parameter>  
            <parameter>  
                <parameterName value="@logger" />  
                <dbType value="String" />  
                <size value="500" />  
                <layout type="log4net.Layout.PatternLayout">  
                    <conversionPattern value="%logger" />  
                </layout>  
            </parameter>  
           <parameter>  
                <parameterName value="@message" />  
                <dbType value="String" />  
                <size value="3000" />  
                <layout type="log4net.Layout.PatternLayout">  
                    <conversionPattern value="%m" />  
                </layout>  
            </parameter>  
            <parameter>  
                <parameterName value="@exception" />  
                <dbType value="String" />  
                <size value="4000" />  
                <layout type="log4net.Layout.ExceptionLayout" />  
            </parameter>  
        </appender>  
        <!-- setup the root category, add the appenders and set the default level --> 
        <root>  
            <level value="WARN"/>  
            <level value="INFO"/>  
            <level value="DEBUG"/>  
            <level value="FINE"/>  
            <appender-ref ref="ADONetAppender" />  
        </root>  
        <!-- specify the level for some specific categories --> 
        <logger name="iNotes">  
            <level value="WARN"/>  
            <level value="INFO"/>  
            <level value="DEBUG"/>  
            <level value="FINE"/>  
            <appender-ref ref="ADONetAppender"/>  
        </logger>  
        <logger name="StellaLogger">  
            <level value="ALL"/>  
            <appender-ref ref="AdoNetAppender" />  
        </logger>  
    </log4net>  
    <!--Log4net End--> 
</configuration>  

 

 

 

 基本完成了!須要注意的一個參數 bufferSize=10  表示是記錄10 條到緩衝區,滿10條後再寫入SQL server,測試時將其改成1,生產環境建議100asp.net

 

CREATE TABLE [dbo].[ErrorLog](   
    [nId] [bigint] IDENTITY(1,1) NOT NULL,   
    [dtDate] [datetime] NOT NULL,   
    [sThread] [nvarchar](100) NOT NULL,   
    [sLevel] [nvarchar](200) NOT NULL,   
    [sLogger] [nvarchar](500) NOT NULL,   
    [sMessage] [nvarchar](3000) NOT NULL,   
    [sException] [nvarchar](4000) NULL) 

調用代碼測試

 

 

 public static class TestLog4
    {
        private static log4net.ILog myLogger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static void OnReceiveMsg()
        {
            myLogger.Debug("系統已記錄錯誤,請聯繫管理員!");
        }
    }
相關文章
相關標籤/搜索