【发布时间】:2010-11-07 03:42:04
【问题描述】:
我正在处理一个 Log4Net 配置,它将记录所有未处理的异常。我需要根据用户将某些属性添加到每个日志条目中。我在 Application_Error 事件中以下列方式成功设置了它。这是我完整的 global.asax
Imports log4net
Imports log4net.Config
Public Class Global_asax
Inherits System.Web.HttpApplication
'Define a static logger variable
Private Shared log As ILog = LogManager.GetLogger(GetType(Global_asax))
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
ConfigureLogging()
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
Dim ex As Exception = Server.GetLastError()
ThreadContext.Properties("user") = User.Identity.Name
ThreadContext.Properties("appbrowser") = String.Concat(Request.Browser.Browser, " ", Request.Browser.Version)
If TypeOf ex Is HttpUnhandledException AndAlso ex.InnerException IsNot Nothing Then
ex = ex.InnerException
End If
log.Error(ex)
ThreadContext.Properties.Clear()
End Sub
Private Sub ConfigureLogging()
Dim logFile As String = Server.MapPath("~/Log4Net.config")
log4net.Config.XmlConfigurator.ConfigureAndWatch(New System.IO.FileInfo(logFile))
log4net.GlobalContext.Properties("appname") = System.Reflection.Assembly.GetExecutingAssembly.GetName.Name
End Sub
End Class
这似乎工作正常。但是,我有一些问题我无法回答。
我通过线程上下文添加用户特定属性的方式是否正确?即使在负载下,这是否总是记录正确的信息?你什么时候使用threadlogicalcontext?有没有更好的方法来做到这一点?
谢谢
【问题讨论】:
标签: asp.net vb.net logging error-handling log4net