c# - 使用 CaSTLe Windsor 注入(inject)日志依赖

标签 c# .net castle-windsor

我试图将 CaSTLe Windsor 的日志记录依赖项注入(inject)到我的代码中。更准确地说,只要类中的方法抛出错误或应用程序流进入方法,它就会简单地记录到一个文件中。如何通过不写类似的东西来做到这一点

logger.Debug("Error code");

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

提前致谢。

最佳答案

使用 interceptor facility在温莎城堡。

所以用

标记你的类(class)
[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 log/rethrow block 中。

关于c# - 使用 CaSTLe Windsor 注入(inject)日志依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7046399/

相关文章:

c# - 为 DateTime 填充 EditorFor

c# - 等待所有工作线程结束

c# - 将 DataGrid 转换为 DataTable

c# - 当我在 C# 中构建 Windows 服务时,为什么我的二进制文件没有放在/bin/release 文件夹中?

dependency-injection - 将连接字符串注入(inject) DI 解析的类

c# - 以编程方式启动 Selenium 测试

c# - 如何测试当前请求是否是 Controller 中的 ajax 请求?

c# - 为什么 TextBlock 不绑定(bind)?

dependency-injection - CaSTLe.Windsor 不会使用 FromAssembly.InThisApplication 运行安装程序,除非在代码中引用程序集

c# - 为什么我的 CaSTLe Windsor Controller 工厂的 GetControllerInstance() 被调用为空值?