Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using C# Custom Attributes for exception and audit trail logging

is it possible to create a custom feature that captures exceptions made in a method that are set by a custom attribute?

im planning to do something like this:

[Logging(FeatureEnum.SomeFeature, IntentEnum.SomeIntent, "some comment")]
public void SomeMethodThatDoesATask()
{
    try
    {
      var doSomeAction = new LazyProcess();
      doSomeAction.WhoDunnit();
    }
    catch(Exception ex)
    {
       StaticMethodThatDoesLogging.CatchError(ex);
    }
}

Question is: How do I capture the Method name where this attribute was placed and what event was thrown? It can either capture an exception or just automatically log that this method was called.

like image 408
Martin Ongtangco Avatar asked Dec 23 '10 07:12

Martin Ongtangco


1 Answers

This cannot be easily achieved. For example TypeMock uses the .NET framework profiler API to monitor an application's execution. It allows you to register for different events and be notified when a method is called, an exception occurs, ... but this is not going to be an easy task.

On the other hand you could use AOP but it requires you to modify your code so that the caller uses some generated proxy instead of the real class. Spring.NET has some nice features about it.

So basically without using the .NET framework Profiler API or without writing some custom code that reads those attributes from a given class using reflection you cannot achieve this. Attributes are just class metadata and without something that would make sense of them they do nothing.

like image 82
Darin Dimitrov Avatar answered Oct 25 '22 06:10

Darin Dimitrov