log4net 将日志写入数据库

 

      asp.net利用log4net写入日志到SqlServer数据库,Log4net是一个开源的错误日志记录项目,易用性强,源自log4j,品质值得信赖。 下面就我的安装部署log4net到MS sql server的经验与大家分享。

  下载log4net的Dll文件并引入到项目中,在你的web主项目(比如MainWeb.csproj)添加对log4net.dll的引用,并在相应的Globall.cs文件中的Application_Start 事件中写入

     

           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文件, 是因为这样可以单独修改调试部署,也就是非侵入式安装的思想。

 

<?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,生产环境建议100

 

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("系统已记录错误,请联系管理员!");
        }
    }

 

posted @ 2014-01-07 15:53  A light heart lives long  阅读(7327)  评论(0编辑  收藏  举报