简体   繁体   English

使用log4net将异常记录到数据库

[英]logging exceptions to database using log4net

I'm currently using log4net to log my messages and exceptions to database. 我目前正在使用log4net将我的消息和例外记录到数据库中。 I'm using this configuration: 我正在使用此配置:

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">

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

  <connectionString value="Data Source=localhost\sqlexpress;Initial Catalog=LogDatabase;User ID=;Password=;"
                    providerName="System.Data.SqlClient" />

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

The logging is working correctly except for the fact that I'm confused about something: If I log an exception using log.Error(ex) then the exception will be logged into the message column, however when I use log.Error("Exception", ex) then the message Exception will be logged to the message column while the exception will be logged to the exception column. 日志记录工作正常,除了我对某些事情感到困惑:如果我使用log.Error(ex)记录异常,那么异常将被记录到消息列中,但是当我使用log.Error("Exception", ex)然后将消息Exception记录到消息列,同时异常将记录到异常列。 I would like to be able to log my exception using log.Error(ex) and to have the message column empty in that case and the exception logged to the actual exception column. 我希望能够使用log.Error(ex)记录我的异常,并在这种情况下将消息列设置为空,并将异常记录到实际的异常列中。

When looking at the log4net documentation you have 2 methods: 查看log4net文档时,您有两种方法:

void Error(Object message)
void Error(Object message, Exception ex)

http://logging.apache.org/log4net/release/sdk/html/M_log4net_ILog_Error.htm http://logging.apache.org/log4net/release/sdk/html/M_log4net_ILog_Error_1.htm http://logging.apache.org/log4net/release/sdk/html/M_log4net_ILog_Error.htm http://logging.apache.org/log4net/release/sdk/html/M_log4net_ILog_Error_1.htm

In both cases the first argument is the message. 在这两种情况下,第一个参数是消息。 If you pass the exception into the message, the exception will be converted to the message (probably to string). 如果将异常传递给消息,则异常将转换为消息(可能转换为字符串)。 Only the second function will fill the exception in the error log: 只有第二个函数将填充错误日志中的异常:

 exception Type: System.Exception The exception to log, including its stack trace. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM