Click here to Skip to main content
13,002,000 members (79,376 online)
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 5 May 2007

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

, 10 Apr 2008
Rate this:
Please Sign up or sign in to vote.
An application to dynamically add logging to your VB/C# applications.

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 to watch for updates.


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


About the Author

Software Developer (Senior) Scratch Audio
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionNot able to run it for web applcaitons Pin
Sweety .23-Mar-14 23:07
memberSweety .23-Mar-14 23:07 
AnswerRe: Not able to run it for web applcaitons Pin
Herbrandson25-Mar-14 7:16
memberHerbrandson25-Mar-14 7:16 
QuestionUsage Pin
jcastor15-Aug-13 3:34
memberjcastor15-Aug-13 3:34 
AnswerRe: Usage Pin
Sweety .24-Mar-14 21:30
memberSweety .24-Mar-14 21:30 
QuestionCan not log windows program. Pin
charliewhit15-Apr-08 7:47
membercharliewhit15-Apr-08 7:47 
GeneralRe: Can not log windows program. Pin
Herbrandson15-Apr-08 13:14
memberHerbrandson15-Apr-08 13:14 
GeneralRe: Can not log windows program. Pin
charliewhit16-Apr-08 3:52
membercharliewhit16-Apr-08 3:52 
GeneralRe: Can not log windows program. Pin
Herbrandson21-Apr-08 4:42
memberHerbrandson21-Apr-08 4:42 
GeneralRe: Can not log windows program. Pin
charliewhit23-Apr-08 4:48
membercharliewhit23-Apr-08 4:48 
GeneralError Pin
Bill Warner14-Apr-08 2:57
memberBill Warner14-Apr-08 2:57 
GeneralRe: Error Pin
Herbrandson14-Apr-08 6:39
memberHerbrandson14-Apr-08 6:39 
GeneralScary... Pin
supercat910-Apr-08 13:30
membersupercat910-Apr-08 13:30 
General"...and still make it home before the kids drive my wife crazy." Pin
Ashaman10-Apr-08 6:35
memberAshaman10-Apr-08 6:35 
GeneralExcellent Pin
merlin9812-Apr-08 5:52
membermerlin9812-Apr-08 5:52 
GeneralRe: Excellent Pin
Herbrandson2-Apr-08 6:28
memberHerbrandson2-Apr-08 6:28 
GeneralGreat stuff here Pin
sefstrat1-Apr-08 12:23
membersefstrat1-Apr-08 12:23 
GeneralBoth download links contain same deliverable. Pin
c-sharp31-Mar-08 15:06
memberc-sharp31-Mar-08 15:06 
GeneralRe: Both download links contain same deliverable. Pin
Herbrandson1-Apr-08 5:26
memberHerbrandson1-Apr-08 5:26 
GeneralLose the extra baggage Pin
rfmobile7-May-07 4:23
memberrfmobile7-May-07 4:23 
GeneralRe: Lose the extra baggage Pin
Herbrandson7-May-07 5:58
memberHerbrandson7-May-07 5:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170624.1 | Last Updated 10 Apr 2008
Article Copyright 2007 by Herbrandson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid