Log4Net,ThreadContext和Global.asax

时间:2009-06-30 21:03:34

标签: asp.net vb.net logging error-handling log4net

我正在开发一个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

这似乎工作正常。但是,我有一些我无法回答的问题。

我通过threadcontext添加用户特定属性的方式是否正确?即使在负载下,这总是会记录正确的信息吗?你什么时候使用threadlogicalcontext?有更好的方法吗?

由于

2 个答案:

答案 0 :(得分:11)

将特定于请求的值加载到ThreadContext中是不安全的。原因是ASP.NET共享线程来处理请求。事实上,它经常这样做。

您可以改为使用LogicalThreadContext,但这只是将值存储在Call Context中,用于Remoting。

AFAIK没有特定于HttpContext的上下文存储,所以你可以做的是将一个“值提供者”实例作为你的线程上下文,并在运行时它将在这个类上调用.ToString()来获取值。 / p>

public class HttpContextUserProvider
{
   public override string ToString()
   {
      return HttpContext.Current.User.Identity.Name;
   }
}

这不太理想,但它确实有效。

答案 1 :(得分:5)

Ben的回答是正确的。

但是,和其他一些用户一样,我仍然有点迷失方向。这个log4net Context problems with ASP.Net thread agility帖子,特别是这个Marek Stój's Blog - log4net Contextual Properties and ASP.NET帖子,通过一些优秀的代码示例为这个问题提供了更多的背景信息。

我强烈推荐MarekStój的实施,但在我的情况下ThreadContext.Properties["UserName"]需要用ThreadContext.Properties["User"]替换。

我在我的Logger类中添加了一个BeginRequest方法,我从Application_AuthenticateRequest调用它来加载所有相关的log4net属性。

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    Logger.BeginRequest(Request);
}

方法代码:

public static void BeginRequest(System.Web.HttpRequest request)
{
    if (request == null) return;

    ThreadContext.Properties["ip_address"] = AdaptivePropertyProvider.Create("ip_address", IPNetworking.GetMachineNameAndIP4Address());
    ThreadContext.Properties["rawUrl"] = AdaptivePropertyProvider.Create("rawUrl", request.RawUrl);

    if (request.Browser != null && request.Browser.Capabilities != null)
        ThreadContext.Properties["browser"] = AdaptivePropertyProvider.Create("browser", request.Browser.Capabilities[""].ToString());

    if (request.IsAuthenticated && HttpContext.Current.User != null)
        ThreadContext.Properties["User"] = AdaptivePropertyProvider.Create("user", HttpContext.Current.User.Identity.Name);
}

我发现我必须传入Request对象,而不是在方法中使用HttpContext.Current.Request。否则我会丢失用户和身份验证信息。请注意,IPNetworking类是我自己的,因此您需要提供自己的获取客户端IP的方法。 AdaptivePropertyProvider课程直接来自MarekStój。