使用C#自定义属性异常和审计跟踪记录 [英] using C# Custom Attributes for exception and audit trail logging

查看:431
本文介绍了使用C#自定义属性异常和审计跟踪记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能创建一个自定义功能,捕捉由一个自定义属性设置的方法制成的例外

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

IM规划做这样的事情:<? / p>

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.

推荐答案

这不能轻易实现。例如 TypeMock 使用.NET Framework探查API来监视应用程序的执行。它允许您注册不同的事件和通知时调用方法时,发生了异常,......但是这不会是一件容易的事。

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.

在另一方面,你可以使用AOP,但它需要你这样,来电者使用一些生成的代理,而不是真正的类来修改代码。 Spring.NET 有关于它的一些不错的功能。

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.

所以基本上不使用.NET框架的探查API或不写,使用反射读取指定类的属性,你不能做到这一点一些自定义代码。属性只是类的元数据,没有的东西,会让他们感觉他们什么都不做。

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.

这篇关于使用C#自定义属性异常和审计跟踪记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆