码迷,mamicode.com
首页 > 数据库 > 详细

log4net写入日志到sqlserver数据库

时间:2017-11-16 14:22:55      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:分享   help   expr   manage   启动   std   程序   创建   自动   

1、添加log4Net配置文件log4net.config  

  配置文件属性设置为:技术分享

  配置如下:

<?xml version="1.0" encoding="utf-8"?>
<log4net debug="false">
  <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="2" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="Data Source=.\SQLExpress;Initial Catalog=testDb;Integrated Security=true" />
    <commandText value="INSERT INTO SysLogs ([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>
  <root>
    <level value="ALL" />
    <appender-ref ref="AdoNetAppender" />
  </root>
</log4net>

2.在数据库创建SysLogs表

3.webConfig 配置

<configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net configSource="log4net.config"/>

4.应用程序启动时设置配置项  Global.asax

   protected void Application_Start()
        {

            //应用程序启动时,自动加载配置log4Net  
            XmlConfigurator.Configure();

        }

5.日志写入示例

public static class LogHelp
    {
        public static void WriteError(Exception ex)
        {
            ILog log = log4net.LogManager.GetLogger("Log");
            log.Error(ex.Message, ex);
        }
    }

 

log4net写入日志到sqlserver数据库

标签:分享   help   expr   manage   启动   std   程序   创建   自动   

原文地址:http://www.cnblogs.com/tangchun/p/7843859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!