Click here to Skip to main content
6,629,377 members and growing! (20,504 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » How To     Advanced License: The Code Project Open License (CPOL)

How to add that missing logging code to your application after it's in the wild

By Herbrandson

An application to dynamically add logging to your VB/C# applications.
C#, VB, .NET, Visual Studio, Dev
Posted:5 May 2007
Updated:10 Apr 2008
Views:20,773
Bookmarked:40 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
28 votes for this article.
Popularity: 5.64 Rating: 3.90 out of 5
2 votes, 10.5%
1
2 votes, 10.5%
2
3 votes, 15.8%
3
3 votes, 15.8%
4
9 votes, 47.4%
5

Logging Woes

Logging is one of those aspects of coding that has always bothered me. It's partly because I hate taking the time to write it, and partly because it can really clutter up otherwise clear code. But mostly it's because anytime I've looked through a log file, trying to figure out why the whozit widget is throwing a null reference exception in the whatzit routine, I end up spending what seems like an eternity sifting through gazillions of mind numbing lines of log files, most of which have no bearing on the issue at hand. And, when I do narrow my hunt down to the appropriate part of the file, I find another surprise. The function I really need info about either seems to be missing all together, or the piece of info that I need isn't included in the logging for that function. Now, what do I do? Well, first, I curse the programmer who wrote this routine… until I realize it was me. Great! Now what? If the bug in question is part of some "live" code, it's not really feasible to quickly code in the logging code that I need, rebuild, and reinstall (or at least not very smart). Depending on the scenario, I may be able to attach a debugger, but that's not always realistic either. I can't really add a break point that stops all code execution in a high traffic web site. So, what now?

A Solution

Logger is an application designed to solve this very issue. Install, select the application you would like to log, select the functions you'd like to log, and start logging. Logger will now generate your missing log info for you. No recompile needed. I can now find the bug (duh, what was I thinking when I wrote that code anyway?), fix it, and still make it home before the kids drive my wife crazy.

How it Works

Logger uses a technique known as "IL Rewriting" to dynamically insert logging code into applications at run time. IL rewriting uses the .NET Profiling API to grab code just before it's JITed. It then alters the IL to include the needed logging code before sending it back on its merry way to the .NET runtime. This technique has the advantage of having a very small performance footprint while at the same time not ever actually altering your binary files.

For a more technical discussion on how this works, see my previous article (from a very rough draft of this application) at Really Easy Logging using IL Rewriting and the .NET Profiling API. Also, check out software.herbrandson.com to watch for updates.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Herbrandson


Member

Occupation: Software Developer (Senior)
Company: AXT Software
Location: United States United States

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
QuestionCan not log vb.net windows program. Pinmembercharliewhit8:47 15 Apr '08  
GeneralRe: Can not log vb.net windows program. PinmemberHerbrandson14:14 15 Apr '08  
GeneralRe: Can not log vb.net windows program. Pinmembercharliewhit4:52 16 Apr '08  
GeneralRe: Can not log vb.net windows program. PinmemberHerbrandson5:42 21 Apr '08  
GeneralRe: Can not log vb.net windows program. Pinmembercharliewhit5:48 23 Apr '08  
GeneralError PinmemberBill Warner3:57 14 Apr '08  
GeneralRe: Error PinmemberHerbrandson7:39 14 Apr '08  
GeneralScary... Pinmembersupercat914:30 10 Apr '08  
General"...and still make it home before the kids drive my wife crazy." PinmemberAshaman7:35 10 Apr '08  
GeneralExcellent Pinmembermerlin9816:52 2 Apr '08  
GeneralRe: Excellent PinmemberHerbrandson7:28 2 Apr '08  
GeneralGreat stuff here Pinmembersefstrat13:23 1 Apr '08  
GeneralBoth download links contain same deliverable. Pinmemberc-sharp16:06 31 Mar '08  
GeneralRe: Both download links contain same deliverable. PinmemberHerbrandson6:26 1 Apr '08  
GeneralLose the extra baggage Pinmemberrfmobile5:23 7 May '07  
GeneralRe: Lose the extra baggage PinmemberHerbrandson6:58 7 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Apr 2008
Editor: Smitha Vijayan
Copyright 2007 by Herbrandson
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project