Enterprise Library 3.1 Logging Formatter Template - Include URL Request

后端 未结 1 1697
情话喂你
情话喂你 2021-01-21 22:16

We have a custom web app built using Ektron v8.0 which uses EL 3.1 and the format template in the logging config is configured as such:



        
1条回答
  •  深忆病人
    2021-01-21 22:53

    There is no template item specifically for the request URL. You can add the request URL to the extended properties yourself so that the information is logged:

    string requestUrl = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
    
    Dictionary dictionary = new Dictionary();
    dictionary.Add("RequestUrl", requestUrl);
    
    Logger.Write("My message", dictionary);
    

    Since the formatter is logging all dictionary key/values your RequestUrl will show up in the log.

    An alternative approach would be to create your own IExtraInformationProvider to populate the specific web information you are interested in. It's really the same thing except using an Enterprise Library interface.

    public class WebContextInformationProvider : IExtraInformationProvider
    {
        public void PopulateDictionary(IDictionary dict)
        {
            dict["RequestUrl"] = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
        }
    }
    
    Dictionary dictionary = new Dictionary();
    WebContextInformationProvider webContext = new WebContextInformationProvider();
    
    webContext.PopulateDictionary(dictionary);
    
    Logger.Write("My message", dictionary);
    

    0 讨论(0)
提交回复
热议问题