Zeus
Zeus

Reputation: 3357

Best way to add logging in existing application (windows or web) .Net

I have inherited couple of .Net (C#) application which does not have any tracing or logging added to it. This application does everything from Creating , Reading , Updating and deleting records. It sends email and calls web services.

Of course it is a nightmare to maintain it because there is no logging and no try catch mechanism (I know I could not believe it either).

So what would be the best way of implementing logging in this system. I can not go to every function call and add logging lines. Is there some way where I can have dynamic logging which can log based on the method names I provide.

I.E. When UpdateOrder() is called , my logger should log (updated order method was called )

Thank you

Upvotes: 3

Views: 3328

Answers (3)

monksy
monksy

Reputation: 14234

Use log4net, it is the .NET version of the Apache log4j. The library is well tested, used by programmers everywhere, and is very customizable. Since you've inherited this code, it would not be a bad thing to go through it and start adding logging support.

The customization settings are handled through a global file within the project with can specify where the logging information goes (Emailed, text files, console) and what level of verbosity is needed per output source of the logging information.

Upvotes: 2

Thomas Levesque
Thomas Levesque

Reputation: 292425

You could use an AOP framework like Postsharp to create a specific attribute to log method calls :

public class TraceAttribute : OnMethodBoundaryAspect 
{ 
  public override void OnEntry( MethodExecutionEventArgs eventArgs) 
  { Trace.TraceInformation("Entering {0}.", eventArgs.Method);  } 

  public override void OnExit( MethodExecutionEventArgs eventArgs) 
  { Trace.TraceInformation("Leaving {0}.", eventArgs.Method);   } 
}

(this code is the example from the home page)

You can then apply this attribute to the methods you want to log :

[Trace]
public void UpdateOrder()
{
    ...
}

Upvotes: 1

John
John

Reputation: 16007

Here's something if you're in .NET:

http://www.codeproject.com/KB/dotnet/LoggerByHernandson.aspx

Upvotes: 0

Related Questions