The best way to add a log to an existing application (Windows or Internet) .Net

I have inherited a couple of .Net applications (C #) that do not contain any traces or logging. This application does everything from creating, reading, updating and deleting records. It sends emails and calls web services.

Of course, this is a nightmare to maintain it, because there is no registration mechanism and no catch mechanism (I know that I also could not believe it).

So what would be the best way to implement registration in this system. I cannot go to every function call and add logging lines. Is there a way that I can have dynamic logging that can be logged based on the names of the methods that I provide.

those. When UpdateOrder () is called, my log should be logged (the updated order method was called)

thanks

+3
source share
3 answers

You can use the AOP framework like Postsharp to create a specific attribute for calls to log methods:

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 an example on the main page)

Then you can apply this attribute to the methods you want to write:

[Trace]
public void UpdateOrder()
{
    ...
}
+1
source

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

, , ( , , ) .

+2
+1
source

Source: https://habr.com/ru/post/1709422/


All Articles