【问题标题】:Inject logging dependency with Castle Windsor使用 Castle Windsor 注入日志记录依赖项
【发布时间】:2011-08-12 21:13:36
【问题描述】:

我试图将 Castle Windsor 的日志依赖注入到我的代码中。更准确地说,每当类中的方法抛出错误或应用程序流进入方法时,它都会简单地登录到文件中。如何通过编写类似的东西来做到这一点

logger.Debug("Error code");

在方法中为每个方法显式。假设我们在每个类或方法上添加属性以利用日志记录工具。

提前致谢。

【问题讨论】:

    标签: c# .net castle-windsor


    【解决方案1】:

    在温莎城堡使用interceptor facility

    所以用

    标记你的班级
    [Interceptor(typeof(UnhandledExceptionLogger))]
    

    并创建一个实现IInterceptor 的类UnhandledExceptionLogger。大致:

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

    然后当 Castle Windsor 创建一个标有 Interceptor 的类的实例时,它将创建一个代理,将所有方法调用包装在上述 try/catch 日志/rethrow 块中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多