log4net将错误保存到db,在参数中获取空值

时间:2011-12-14 14:35:57

标签: asp.net vb.net log4net

在SQL Server 2005数据库中插入行时,我在Environment,ApplicationName,ApplicationPath,ExceptionData中获取空值。

我的log4net配置如下:

<log4net>
  <appender name="ADONetAppender_SqlServer" 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=xxx;Initial Catalog=xxx;User Id=xxx;Password=xx;Connection Timeout=200;" />
  <commandText value="INSERT INTO [ApplicationLog] ([TimeStamp],[SeverityLevel],[LoggerName],[Environment],[ApplicationName],[ApplicationPath],[ServerName],[Message],[ExceptionData]) VALUES (@TimeStamp,@SeverityLevel,@LoggerName,@Environment,@ApplicationName,@ApplicationPath,@ServerName,@Message,@ExceptionData)" />
    <parameter>
      <parameterName value="@TimeStamp" />
      <dbType value="DateTime" />
      <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
      <parameterName value="@SeverityLevel" />
      <dbType value="String" />
      <size value="10" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@LoggerName" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%logger" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@Environment" />
      <dbType value="String" />
      <size value="-1" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{EnvironmentName}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@ApplicationName" />
      <dbType value="String" />
      <size value="128" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{AppName}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@ApplicationPath" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{AppPath}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@ServerName" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{log4net:HostName}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@Message" />
      <dbType value="String" />
      <size value="-1" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@ExceptionData" />
      <dbType value="String" />
      <size value="-1" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{exceptionDetails}" />
      </layout>
    </parameter>
</appender>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  <to value="xxx" />
  <from value="xxx" />
  <subject value="(Error) xxx" />
  <smtpHost value="xxx" />
  <bufferSize value="1" />
  <lossy value="false" />
  <threshold value="ERROR" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%utcdate [%thread] %-5level %logger [%property{log4net:HostName}] - %message%newline%newlineException details:%newline%property{exceptionDetails}" />
  </layout>
</appender>

<root>
  <level value="INFO" />
  <appender-ref ref="ADONetAppender_SqlServer" />
  <appender-ref ref="SmtpAppender" />
</root>

在global.asax,Application_Error中,我调用log4net进行配置,然后按如下方式记录异常:

Dim ex As New Exception
ex = Server.GetLastError      

log4net.Config.XmlConfigurator.Configure()
log.Logger.Log(ex.Source.GetType, log4net.Core.Level.Fatal, ex.StackTrace, ex)

我做错了什么?

1 个答案:

答案 0 :(得分:1)

由于您没有明确说明或者如何初始化它们,我猜您认为Environment,ApplicationName,ApplicationPath和ExceptionData是预定义的属性,我认为这些属性都不正确。

因此,如果要在代码中的某个位置使用这些属性,则需要将这些属性设置为所需的值。例如

 log4net.GlobalContext.Properties["AppName"] = "...";

 log4net.ThreadContext.Properties["exceptionDetails] = "...";

某些变量可能是应用程序的全局变量,因此可以设置一次,但是应该在调用log.Logger.Log之前设置exceptiondetails,并且应该使用ThreadContext而不是GlobalContext。

See here for more information on the various contexts

注意,您可以使用一些pre defined patterns代替。例如%exception或%appdomain