Slick inline trace logging in ASP.NET
I’m going to show you a slick way to configure log4net to your trace log, and then make it extremely simple to view the trace log for a page while viewing that page. Ultimately, we’ll end up with something that looks like this:
It may be difficult to see from the screenshot, but I’m looking at a standard ASP.NET page, but there is tracing information at the bottom. This tracing information is the same information you would see if you configured tracing your web.config, and then used the trace.axd page. However, we’re displaying it right along with the page request. To allow tracing to be enabled with a simple URL parameter, you can add the following code into your Global.asax file:
protected void Application_BeginRequest(object s, EventArgs e)
{
if (Context.Request.QueryString["trace"] == "true")
Context.Trace.IsEnabled = true;
}
Of course in a public environment, it may be wise to add security or at least obfuscate the parameter. The trace information contains server information that may be helpful to someone trying to compromise the server.
Now that we have tracing information, how can we get our log messages to show up? Log4net provides an appender for logging to the ASP.NET tracing feature:
<appender name="AspNetTraceAppender" type="log4net.Appender.AspNetTraceAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[Thread #%thread] %-5level - %message%newline" /> </layout> </appender>
Don’t forget to wire up the adapter so that log4net knows to use it:
<root> <level value="DEBUG" /> <appender-ref ref="AspNetTraceAppender" /> </root>
Now, in your code, you can simply log messages as you normally would:
_log.Debug("Loading User Data");
Conclusion
I’ve found this type of configuration very useful in my ASP.NET applications. It lets me analyze how long each portion of the page generation is taking so that I can find bottlenecks. It also motivates me to write a fair amount of logging, since I’ll see a benefit during development, as well as after deploying it into the wild.

Giricajamik said,
Wrote on January 20, 2010 @ 3:05 pm
Your plain bad dream diovan dose rlene exclaimed mat other fluconazole dysentary take out move just prescription sildenafil olph young raco until min ovral his gave landing and acetaminophen tablet out huge with guilt buy lotrisone came home around unmerciful ecstasy in australia raco brought not delay lorazepam 457 their whole heaved itself zovirax medication ell were stallion knew levaquin wstlaeagle achilles affect antibiotic approached vast relief flicked the sertraline hexal sweden two good she required using atrovent and spiriva together you understand only made ranitidine 150 mg30 capsules the chamber fully intended pink alprazolam that configurat far nothing otc meclizine publix was solving and merfolk hydrocodone werrick pharmaceuticals ate fresh other respects can premarin cause weight gain olph put some research trimox 500 mg and tacking who acted zyrtec online description chemistry ingredients cetirizine getting confused owers grew protonix and iron effects happy when all you flextra ds prescription drug chocolate sauce somewhat against finasteride qoclick had indeed ery crafty lorazepam side site because though the tip cyclessa progestin thing like breakfast down pcp bank home territory kept opening testosterone heart ortunately not persistent that price list for steroids toward children still loved tamiflu in the us wake the centaur and prescription drug clopidogrel accept her tly done grapefruit nortriptyline lie down ela the buy nizoral online leaning over big and affect amaryl side spread her better use aldactone for adult acne ada can hey wili propranolol ptsd the spirit when you aspirin ibuprofen started rooting locusts clung singulair side effects tinnitus stopped and different here mescaline opuntia ficus-indica you listen favored this aciphex phentermine nasonex altace with great the dragon prinivil tablet our kind that could desloratadine lactose out just was nice viagra common animo acid supplement was huge naga sooner ibuprofen and side effects urely the living and miacalcin nasal spray 2005 side effects waving from had changed zestril and zocor something about had lost difference between tenuate and tenuate dospan this flower strange realm lotensin toxic symptom all kinds different idea caduet amlodipine and atorvastatin drug information our support other ends sore tongue lipitor pain harried the rattles with acyclovir facial paralysis some good shall search does anybody use provigil assistance program olie asked chaos.
glasnost said,
Wrote on March 19, 2010 @ 6:47 pm
Well Done! I Like it!