2011-02-09 49 views
2

我們有一個自定義的Web應用程序內置利用使用EL 3.1曄V8.0,並在日誌配置的格式模板被配置爲這樣:企業庫3.1日誌格式化模板 - 包括URL請求

<add 
     name="Text Formatter" 
     type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging" 
     template="Timestamp: {timestamp} 
Message: {message} 
Category: {category} 
Priority: {priority} 
EventId: {eventid} 
Severity: {severity} 
Title:{title} 
Extended Properties: {dictionary({key} - {value} 
)}" 
       /> 

是有請求URL的模板項嗎?如果沒有帶查詢字符串參數的請求url,則很難調試錯誤。

回答

1

沒有專門用於請求URL的模板項目。你可以請求URL自己添加到擴展屬性,使得信息將被記錄:

string requestUrl = System.Web.HttpContext.Current.Request.Url.AbsoluteUri; 

Dictionary<string, object> dictionary = new Dictionary<string, object>(); 
dictionary.Add("RequestUrl", requestUrl); 

Logger.Write("My message", dictionary); 

因爲格式化時記錄所有字典鍵/值的RequestUrl將在日誌中顯示出來。

另一種方法是創建您自己的IExtraInformationProvider來填充您感興趣的特定Web信息。除了使用企業庫接口外,其實是一樣的。

public class WebContextInformationProvider : IExtraInformationProvider 
{ 
    public void PopulateDictionary(IDictionary<string, object> dict) 
    { 
     dict["RequestUrl"] = System.Web.HttpContext.Current.Request.Url.AbsoluteUri; 
    } 
} 

Dictionary<string, object> dictionary = new Dictionary<string, object>(); 
WebContextInformationProvider webContext = new WebContextInformationProvider(); 

webContext.PopulateDictionary(dictionary); 

Logger.Write("My message", dictionary); 
+0

我看了看3.1日誌MSDN文章,我沒有看到那裏的文檔的模板。有任何想法嗎? 我會接受你的答案,因爲我認爲這是唯一可行的解​​決方案。感謝您提供一個清晰,簡潔的答案。 – 2011-02-10 16:04:21