Enterprise Library 3.1 Logging Formatter Template – Include URL Request

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:

<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}
)}"
                />

Is there a template item for Request URL? Without the request url with querystring parameters, it’s difficult to debug errors.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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<string, object> dictionary = new Dictionary<string, object>();
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<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);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x