首页 > 代码库 > Asp.net 使用log4net日志功能

Asp.net 使用log4net日志功能

 

  Asp.net使用log4net日志功能

1. bin文件夹下面引入 log4net.dll。你只需要引入log4net.dll 既可,其他dll是我抠图的时候扣多了

 

log4netdll.png

 

2 . Global.asax 中加入

   void Application_Start(object sender,EventArgs e)

    {

        // 在应用程序启动时运行的代码

       log4net.Config.DOMConfigurator.Configure();

    }

 

3.修改配置文件(web.config)

   log日志既可以写到数据库,也可以写入文本文件中

  

  (1) .

      configSections 中加入如下配置节,及引入log4net的处理程序集

        <configSections>

         <sectionname="log4net"type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

    </configSections>

 

  (2). configuration下面加入下面的代码(整个复制即可)

  
      <log4net>

 

                   <root>

                            <level value="ALL"/>

                            <appender-ref ref="ADONetAppender"/>

                   </root>

 

       <!--

       相关操作代码

     -->

                   <appendername="ADONetAppender" type="log4net.Appender.ADONetAppender">

                            <bufferSize value="0"/>

                            <connectionType value="System.Data.SqlClient.SqlConnection,System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

                            <connectionString value="server=.;database=test;userid=newst;password=xxx" />

                            <commandText value="INSERTINTO 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>

         </log4net>

 

 

以上的这个配置是用于保存到数据库的,

最后你要再数据库中建立一张如下的表

   CREATE TABLE [Log]

 (

     [Id] [int] IDENTITY(1, 1) NOT NULL,

     [Date] [datetime] NOTNULL,

     [Thread] [varchar] (255) NOTNULL,

     [Level] [varchar] (50) NOT NULL,

     [Logger] [varchar] (255) NOTNULL,

     [Message] [varchar] (4000) NOT NULL,

     [Exception] [varchar] (2000) NULL

 )

 

 

上面的配置节是针对数据库保存方式

如果你想要保存到你项目的文件中,请使用下面的配置节

<log4net>

 

                   <root>

                            <levelvalue="ALL" />

                            <appender-refref="LogFileAppender" />

                   </root>

 

                   <!--

       相关操作代码

     -->

 

                   <appendername="LogFileAppender"  type="log4net.Appender.FileAppender" >

                            <paramname="File"value="Log\log-file.txt" />        //日志文件保存的地方

                            <paramname="AppendToFile"value="true" />

                            <layouttype="log4net.Layout.PatternLayout">

                                     <paramname="ConversionPattern"  value="记录时间:%d     线程 ID:[%t]    日志级别:%-5p     出错类:%logger property:[%property{NDC}]     错误描述:%m%n" />

                            </layout>

                   </appender>

         </log4net>

   

上面的配置节将把你的日志信息写入 log-file.txt文件中 ,如果项目没有这个文件,没关心log4net 会自动创建。

 

 

4 最后一步就是如何使用log4net

   新建一个类文件,引入如下代码
 

  public class MyLog

{

    privatestatic readonlylog4net.ILog log = log4net.LogManager.GetLogger(typeof(MyLog));

 

    //日志文件类

    publicMyLog()

    {

        //

        //TODO: 在此处添加构造函数逻辑

        //

    }

 

    publicstatic voidlogInfor(string infor)

    {

        log.Info(infor);

    }

 

    publicstatic voidErrorInfor(string errInfor)

    {

        log.Info(errInfor);

    }

 

}

 

然后在你需要使用的地方 调用即可

很简单吧。


本文出自 “零点” 博客,请务必保留此出处http://5041017.blog.51cto.com/244240/1566489

Asp.net 使用log4net日志功能