using C# Custom Attributes for exception and audit trail logging

久未见 提交于 2019-12-21 09:33:38

问题


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.


回答1:


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.




回答2:


Have a look at

http://www.dcl.hpi.uni-potsdam.de/research/loom/rapier_loom.htm

http://www.dcl.hpi.uni-potsdam.de/research/loom/gripper_loom.htm

First is runtime weaver while second one is static weaver.

These are another good resources:

http://ayende.com/Blog/archive/2007/07/02/7-Approaches-for-AOP-in-.Net.aspx

http://msdn.microsoft.com/en-us/magazine/cc301356.aspx

http://www.codeproject.com/KB/architecture/AOP_Injection_IL.aspx




回答3:


Attributes are just metadata. You would need to do code-weaving, using something like PostSharp, or use a runtime interception library such as Castle.DynamicProxy. By themselves attributes contain no real functionality to the application except through reflection.

If you have a line of code inside the method that does the logging, you can get the stackframe of the calling method, use reflection to check for the attribute, and go from there. I am assuming this is what you wanted to do with StaticMethodThatDoesLogging.

public void CatchError(Exception ex)
{
    StackFrame parentFrame = new StackFrame(1);
    MethodBase mi = parentFrame.GetMethod();
    LoggingAttribute attr = mi.GetCustomAttributes(typeof(LoggingAttribute), true).FirstOrDefault() as LoggingAttribute;

    if (attr != null)
    {
       // do your logging.
    }
}


来源:https://stackoverflow.com/questions/4516769/using-c-sharp-custom-attributes-for-exception-and-audit-trail-logging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!