Umbraco 如何将log4net-aspnet请求转换模式用于关联id

Umbraco 如何将log4net-aspnet请求转换模式用于关联id,umbraco,log4net,Umbraco,Log4net,我希望将分配给每个错误的相关id放入由log4net生成的日志文件中。我希望这样,这样我就可以在自定义错误页面中向用户显示它,然后我们的ops可以匹配它们,并知道哪个错误是哪个 我设置了一个无操作过滤器属性: public class RequestModificationForLoggingFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filt

我希望将分配给每个错误的相关id放入由log4net生成的日志文件中。我希望这样,这样我就可以在自定义错误页面中向用户显示它,然后我们的ops可以匹配它们,并知道哪个错误是哪个

我设置了一个无操作过滤器属性:

public class RequestModificationForLoggingFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext.Current.Request.Headers["correlationid"] = Guid.NewGuid().ToString();
    }
}
并将log4net.config更改为(注意
[%aspnet请求{correlationid}]
):


我哪里出错了?

我最终创建了一个模块:

public class LoggingModule : IHttpModule
{
    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(OnBeginRequest);
    }

    public void OnBeginRequest(Object sender, EventArgs e)
    {
        // When the error event is called, we set a correlation id which gets used in the logging
        ThreadContext.Properties["correlationid"] = Guid.NewGuid().ToString();
    }
}
然后使用这个log4net语法:

%property{correlationid}
不要忘记,如果您想要从applicationstartup或类似的东西跟踪的任何东西的id,您可以在跟踪侦听器中进行检查,如:

// If correlationid is not set, then this log was thrown from a place where a request was not made. ie. OnApplicationStarting
if (ThreadContext.Properties["correlationid"] == null)
{
    // We assign an id here to ensure the log does not show null for id
    ThreadContext.Properties["correlationid"] = Guid.NewGuid().ToString();
}
%property{correlationid}
// If correlationid is not set, then this log was thrown from a place where a request was not made. ie. OnApplicationStarting
if (ThreadContext.Properties["correlationid"] == null)
{
    // We assign an id here to ensure the log does not show null for id
    ThreadContext.Properties["correlationid"] = Guid.NewGuid().ToString();
}