Programming Languages Hacks

Importanti regole per linguaggi di programmazione rilevanti come Java, C, C++, C#…

  • Subscribe

  • Lettori

    I miei lettori abituali

  • Twitter

Log4Net Custom Properties

Posted by Ricibald on November 8th, 2013

Log4Net Log Custom Properties

From Tim Lewis’s answer on stackoverflow:

Since ASP.NET does not guarantee that the entire page request will be processed on the same thread, I prefer getting the answer from HttpContext.Current as log4net processes the logging event.

The following GetCurrentPage class implements what the log4net manual calls an “Active Property Value” by overriding its ToString method:

public class GetCurrentPage
{
  public override string ToString()
  {
      if (null != HttpContext.Current)
      {
          return HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;
      }
      return string.Empty; // or "[No Page]" if you prefer
  }
}

Register this class in Global.asax’s Application_Start in log4net’s GlobalContext.

protected void Application_Start(object sender, EventArgs e)
{
    XmlConfigurator.Configure();
    GlobalContext.Properties["page"] = new GetCurrentPage();
}

When log4net writes the %property{page} part of the line it will call the ToString method of our GetCurrentPage class which will lookup the value in the current request.

The same is possible to log the current HttpContext.

5 Responses to “Log4Net Custom Properties”

  1. stylin trucks Says:

    Is really attention-grabbing, You are an overly expert blogger. I have became a member of the rss feed and look toward seeking much more of your own wonderful submit. Moreover, I’ve got discussed your internet site around my internet sites

  2. Sacramento county jail Inmate search Says:

    Wow, this post is pleasant, my sister is analyzing these kinds of things,
    so I am going to convey her.

    Feel free to visit my blog … Sacramento county jail Inmate search

  3. learn more Says:

    go here for the best innovation wireless available

  4. mike chang six pack shortcuts Says:

    Very good site you have here but I was curious if you knew of any discussion boards that
    cover the same topics talked about here? I’d really like to be
    a part of community where I can get feed-back from
    other experienced individuals that share the
    same interest. If you have any suggestions,
    please let me know. Thank you!

  5. Harris County Inmates Says:

    We’re no longer good where you’re getting the information and facts, although great subject. I personally needs to spend quite some time researching far more or maybe working out more.. harris county inmates Thanks for great facts I had been searching for this review in my vision.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>