gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:32:04 25 4
gpt4 key购买 nike

我试图将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com