tags:

views:

337

answers:

3

Hi guys,

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

+1  A: 

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

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

John at CashCommons
+1 I guess you're using .net because of your method name
victor hugo
+1  A: 

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()
{
    ...
}
Thomas Levesque
PostSharp is a very nice solution for this problem. The attribute has the downside of having to modify the code, though. A better (less intrusive) way to do this would be to use the MultiCastAttribute in PostSharp, where you only have to specify ONE attribute where you tell what types (or methods) are affected:[assembly: MyOnMethodInvocationAspect( AttributeTargetAssemblies = "mscorlib", AttributeTargetTypes = "System.Threading.*")]will make postsharp intercept every call to any class in System.Threading.* (see more in http://doc.postsharp.org/ )
Yann Schwartz
+1  A: 

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.

monksy