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)
    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.

8 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.

  6. hcso inmate search Says:

    Thanks for some other educational internet site hcso inmate search. Wherever in addition may I am getting in which form of information coded in this sort of perfect style? I’ve a job i’m only now concentrating on, and I’ve already been in the start looking away intended for such information.

  7. motorbikes games Says:

    You’ll be able to add this recreation to the section of your
    favorite video games, and you’ll not look for
    it on the Web the subsequent time.

  8. Harris County Inmate Search Says:

    Appreciation for various other insightful site. Where altogether different could possibly I am getting this variety of information designed in this sort of great approach? I own a project that we are only now focusing on, and I’ve ended up within the start looking out and about for similarly info.. harris county inmate search

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>