注射记录依赖与温莎城堡 [英] Inject logging dependency with Castle Windsor

查看:123
本文介绍了注射记录依赖与温莎城堡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过温莎城堡到我的code注射记录的依赖。更多precisely,每当在一个类中的方法抛出错误或应用程序的流量进入它只是记录到文件的方法。如何通过写作没有像

I was trying to inject logging dependency by Castle Windsor to my code. More precisely, whenever a method in a class throws an error or application flow enters into a method it simply logs into a file. How to do this by writing nothing like

logger.Debug("Error code");

的方法中明确地为每个方法。假设我们添加在每个类或方法的属性来利用日志记录功能为。

in the methods explicitly for each of the methods. Suppose we add attribute on each of the class or methods to leverage the logging facility for that.

在此先感谢。

推荐答案

使用的在温莎城堡的拦截设施

因此​​,与

[Interceptor(typeof(UnhandledExceptionLogger))]

和创建一个类 UnhandledExceptionLogger 实现 IInterceptor 。大致是:

and create a class UnhandledExceptionLogger that implements IInterceptor. Roughly:

public void Intercept(IInvocation invocation) {
    try {                
        invocation.Proceed();
    }
    catch (Exception e) {
        // log the exception e
        throw;
    }
}

然后在温莎城堡创建你的类,标有该实例拦截,它会创建一个代理,它包装的所有方法调用在上面的try / catch日志/重新抛出块。

and then when Castle Windsor creates an instance of your class marked with this Interceptor, it will create a proxy that wraps all methods invocations in the above try/catch log/rethrow block.

这篇关于注射记录依赖与温莎城堡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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