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

Log4Net记录到数据库

时间:2018-06-11 10:51:31      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:color   ons   gty   for   category   ati   comm   ogre   sage   

WinForm下Log4Net的配置

Log4Net 组件下载地址

https://download.csdn.net/download/zgx123zgx123zg/10470986

configSections节点下添加

    <!--Log4Net配置-->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

configuration节点下添加

<log4net>
    <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="Data Source=.;Initial Catalog=Test;Integrated Security=True;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>

AssemblyInfo.cs文件下添加

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

添加一个按钮点击事件里加入

         log4net.ILog myLogger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            myLogger.Warn("这是一个警告日志");
            myLogger.Info("单击了按钮");
            myLogger.Debug("用Log4Net写入数据库日志");
            myLogger.Error("这是一个错误日志");
            myLogger.Fatal("这是一个致命的错误日志");
            try
            {
                ILog logger = LogManager.GetLogger("LogRecord");
                logger.Debug("用Log4Net写入数据库日志");
                int x = 5;
                int y = 0;
                throw new Exception("测试错误");
            }
            catch (Exception ex)
            {
                myLogger.Error(ex.Message);
            }
            finally
            {
                label1.Text = DateTime.Now.ToLongTimeString() + ":操作日志已记录完成";
            }

转载自http://www.cnblogs.com/wolf-sun/p/3347373.html#3009010

Log4Net记录到数据库

标签:color   ons   gty   for   category   ati   comm   ogre   sage   

原文地址:https://www.cnblogs.com/RambleLife/p/9165248.html

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