我們正在使用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}

Category: {category}

Priority: {priority}

EventId: {eventid}

Severity: {severity}

Title:{title}

Machine: {machine}

Application Domain: {appDomain}

Process Id: {processId}

Process Name: {processName}

Win32 Thread Id: {win32ThreadId}

Thread Name: {threadName}

Extended Properties: {dictionary({key} - {value}
)}
"
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 ;
}
所以在測試環境中,它只是不被翻譯或ExtendedProperties令牌所有的令牌? –