欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Log4Net记录到数据库

程序员文章站 2022-11-01 11:40:01
WinForm下Log4Net的配置 Log4Net 组件下载地址 https://download.csdn.net/download/zgx123zgx123zg/10470986 configSections节点下添加 configuration节点下添加 AssemblyInfo.cs文件下 ......

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