2013-12-11 52 views
1

我們正在使用MS Enterprise Library 5.0登錄我們的網站。Microsoft企業庫日誌記錄應用程序塊格式錯誤

而當異常處理未處理時,我們希望將它與一段上下文信息一起記錄以幫助調試問題。因此,我們有一個位於網站頂部的HttpModule,其作用是捕獲這些內容並將它們記錄到Windows應用程序事件日誌中。這是非常香草的東西。

它在我們的生產Web服務器上工作。它在我們的測試Web服務器上無法正常工作。

問題是LogEvent.ExtendedProperties集合未正確呈現(或根本不呈現)。取而代之的是,替換令牌只是逐字地轉存到事件日誌中。

生產事件日誌顯示如下(略有刪節)信息,以格式化的一切你所期望的方式,它是:

12/10/2013 06:07:13 PM 
LogName=Application 
SourceName=Secure Website 
EventCode=1729 
EventType=2 
Type=Error 
ComputerName=WSSECURE09.website.nordstrom.com 
TaskCategory=%1 
OpCode=Info 
RecordNumber=220338 
Keywords=Classic 
Message=Message: Unhandled exception in Secure Website: System.ServiceModel.FaultException`1[Nordstrom.Contracts.Fault.ServiceFault]: 
    Argument shopper is invalid. 
    Email is null or empty. 
    (Fault Detail is equal to Error code: InvalidArguments). 
. 
. 
. 
Extended Properties: Shopper ID - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 

在最後一行(S)(Extended Properties: ...)轉儲ExtendedProperties收集並給出遇到問題的購物者的ID以幫助調試它。在我們的測試環境中,擴展屬性集合根本不會被拋棄。相反,從模板的原始令牌逐字寫出,沒有進行換人:

Extended Properties: {key} - {value} 
)} 

這裏是正在使用的格式模板,直接從web.config文件。我已經添加了可讀性換行,但是這是唯一的變化:

<formatters> 
    <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging" 
    template=" 
     Message: {message}&#xA; 
     Category: {category}&#xA; 
     Priority: {priority}&#xA; 
     EventId: {eventid}&#xA; 
     Severity: {severity}&#xA; 
     Title:{title}&#xA; 
     Machine: {machine}&#xA; 
     Application Domain: {appDomain}&#xA; 
     Process Id: {processId}&#xA; 
     Process Name: {processName}&#xA; 
     Win32 Thread Id: {win32ThreadId}&#xA; 
     Thread Name: {threadName}&#xA; 
     Extended Properties: {dictionary({key} - {value}&#xA;)} 
     " 
    name="Text Formatter" 
    /> 
</formatters> 

而這裏的[略消毒]日誌代碼:

private static void LogHandledException(Exception ex) 
{ 
    HttpContext context = HttpContext.Current ; 
    LogEntry entry = new LogEntry() ; 

    entry.Categories.Add("Errors"); 
    entry.Priority = 1; 
    entry.Severity = TraceEventType.Critical; 
    entry.Message = string.Format("Unhandled exception in Secure Website: {0}" , ex) ; 
    entry.EventId = (int) EventId.UnhandledException ; 

    // Add shopper id to the log, if we can find it in the http context 
    Shopper shopper = HttpContext.Current.Items[ "shopper" ] as Shopper ; 
    if (shopper != null) 
    { 
    bool hasShopperId = ! string.IsNullOrWhiteSpace(shopper.Id) ; 
    if (hasShopperId) 
    { 
     KeyValuePair<string,object> item = new KeyValuePair<string,object>("ShopperID",shopper.Id) ; 
     entry.ExtendedProperties.Add(item) ; 
    } 
    } 

    Logger.Write(entry); 
    return ; 
} 
+0

所以在測試環境中,它只是不被翻譯或ExtendedProperties令牌所有的令牌? –

回答

1

它的工作,我們的配置管理團隊已經推出了自己的工具做配置文件轉換:

該工具的解析器顯然是不正確的......寫。嵌套的大括號似乎混淆了它。爲什麼它會在字符串文本中解析,或者爲什麼它可以正確地爲我們的生產環境轉換,但不適用於任何其他環境的轉換,我不知道,但CM團隊修復了他們的工具。 Et瞧!問題解決了。

我沒有想到我們的構建工具會被破壞(或者我們已經推出了自己的標準,有可用的工具來完成這項工作),但是一旦我排除了我們的代碼,並排除了不同服務器上的配置差異,這並沒有爲其他任何方面留下太多空間。福爾摩斯說,

「多久有我對你說,當你排除了所有的不可能,無論 遺體,即使是不可能也一定是真相呢?我們知道,他沒有來過 門,窗戶或煙囪。我們也知道他不可能在房間裏隱藏着 ,因爲沒有隱瞞的可能。那麼,什麼時候,他纔來了?」
—福爾摩斯的跡象,柯南道爾爵士

相關問題