zoukankan      html  css  js  c++  java
  • log4net写入日志到sqlserver数据库

    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);
            }
        }
  • 相关阅读:
    windows下配置mysql环境变量
    360Top奢侈品演示站
    CSS3性能体验
    使用jquery获取radio的值
    获取select 的 val 和 text [转引]
    PHP时间比较和时间差如何计算
    InnoDB和MyISAM的区别与选择
    大型网站技术架构学习摘要
    大型网站系统架构分析--转
    一步步构建大型网站架构-转
  • 原文地址:https://www.cnblogs.com/tangchun/p/7843859.html
Copyright © 2011-2022 走看看