2017-03-21 8 views
0

log4net에 의해 생성 된 로그 파일에있는 각 오류에 상관 ID가 할당되도록하고 싶습니다. 사용자 정의 오류 페이지에서 사용자에게 표시 할 수 있도록이 작업을 수행합니다. 그러면 작업이 일치하여 오류가 무엇인지 알 수 있습니다.상관 ID에 log4net aspnet-request 변환 패턴을 사용하는 방법

은 내가 더 ActionFilterAttribute 설정하지 :

public class RequestModificationForLoggingFilter : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     HttpContext.Current.Request.Headers["correlationid"] = Guid.NewGuid().ToString(); 
    } 
} 

과의 log4net.config (차리는 [%aspnet-request{correlationid}]) 변경 :

<conversionPattern value=" %utcdate [%aspnet-request{correlationid}] [P%property{processId}/D%property{appDomainId}/T%thread] %-5level %logger - %message%newline" /> 

을하지만 로그는 말을 끝 [이용 불가] :

2017-03-21 14:40:18,151 [NOT AVAILABLE] [P54916/D3/T83] WARN - blahblahblah 

어디로 잘못 가고 있습니까?

답변

0

내가 모듈 생성 결국 :

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} 

당신은 아무것도에 대한 ID를 원하는 경우 applicationstartup에서 추적되는 것을 잊지 마십시오 다음과 같이 추적 수신기에 수표를 넣을 수 있습니다.

// 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(); 
}