Capturing events for logging

When the application starts, I need to know certain methods when they start. How can I do this using AOP attributes and methods?

The easiest way is to write the time in the event method, for example:

private void Page_load()
{
  DateTime dt = DateTime.Now;
}

      

And store the Datetime in the database. But this is definitely undesirable as it will result in a lot of end-to-end functionality in this method, making the maintenance work easier. I am thinking about using attributes to solve this problem. PostSharp seems to be a good candidate here as it can intercept method calls and do whatever preprocessing and post-processing you want. But one thing that is clearly missing is that it cannot handle events without me writing a lot of custom code.

Is there any framework that can handle events normally?

+1


source to share


3 answers


Postsharp can help you here



+2


source


You don't need to have a separate method for every event you want to record this way.

Write one way of logging:

public static void LogEventRaised(string event)
{
    ...
}

      



then subscribe to events using anonymous method:

Load += delegate { LogEventRaised("Load") };

      

Yes, it is more imperative than declarative and repeats the name of the event, but it is still quite compact. You can attach handlers to all events using reflection, but that would probably be overkill.

+1


source


The Spring framework has an aspect-oriented programming module that provides logging support.

+1


source







All Articles