2011-12-14 44 views
1

當在SQL Server 2005數據庫中插入行時,我在Environment,ApplicationName,ApplicationPath,ExceptionData中獲取空值。log4net將錯誤保存到數據庫,獲取參數中的空值

我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

由於您沒有明確地表示否則或顯示如何初始化它們,我猜你認爲Environment,ApplicationName,ApplicationPath和ExceptionData是預定義的屬性,我不認爲這是真的。

所以,如果你想使用這些屬性,那麼在你的代碼中的某個地方,你需要將這些屬性設置爲你想要的值。如剛剛在調用log.Logger.Log並應使用

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

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

有些變量可能是全球性的,以您的應用程序,因此可以設置一次,但exceptiondetails應設置ThreadContext而不是GlobalContext。

See here for more information on the various contexts

注意,有一些pre defined patterns你或許可以改爲使用。例如%例外或%appdomain

相關問題