使用 Castle 记录 WCF 中抛出的异常(以及其他情况)

发布于 2024-07-16 09:59:37 字数 420 浏览 7 评论 0原文

我正在编写一个 WCF Web 服务,我想知道是否有一种优雅的(面向方面​​的)方法来使用 Castle 拦截器机制来记录我的 Web 方法抛出的异常? 我知道 IInterceptor 接口,但我在那里找不到任何异常信息。

我见过 Castle、AOP 和 .NET 中的日志记录,但它只是涵盖方法的参数。

是否有更好的(WCF 或 Castle 的)机制来执行此操作? 顺便说一句,我正在使用 log4net 进行日志记录。

更新:我了解 WCF 的日志记录工具,但我更多地寻找通用解决方案,而不仅仅是在 WCF 环境中。

提前致谢。

I'm writing a WCF web service and I'm wondering if there's an elegant (aspect-oriented) way to use Castle interceptor mechanism for logging exceptions thrown by my web methods? I know about the IInterceptor inteface, but I could not find any exception information there.

I've seen Castle, AOP and Logging in .NET, but it only covers method's parameters.

Is there a better (WCF or Castle's) mechanism for doing this? BTW I'm using log4net for logging.

UPDATE: I know about WCF's logging facilities, but I was looking more for a general solution, not just in WCF environment.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金兰素衣 2024-07-23 09:59:37

如果你想一般使用 Castle Interceptors,你可以这样做:

public void Intercept(IInvocation invocation)
{
   //do some preprocessing here if needed
   try
   {
      invocation.Proceed()
   }
   catch(Exception e)
   {
      LogException(e);
      throw;
   }
   finally
   {
      //do some postprocessing jf needed
   }
}

If you want to use Castle Interceptors in general, you could do something like this:

public void Intercept(IInvocation invocation)
{
   //do some preprocessing here if needed
   try
   {
      invocation.Proceed()
   }
   catch(Exception e)
   {
      LogException(e);
      throw;
   }
   finally
   {
      //do some postprocessing jf needed
   }
}
谈情不如逗狗 2024-07-23 09:59:37

您应该在 WCF 跟踪和日志记录中使用构建。 然后,您可以使用 WCF 跟踪查看器,它将来自服务和客户端的日志拼凑在一起,以便您可以看到完整的消息流。

http://www.devx.com/dotnet/Article/37389/ 0/page/6

WCF中有IErrorHandler接口:

public interface IErrorHandler
{
    bool HandleError(Exception error, MessageFault fault);
    void ProvideFault(Exception error, ref MessageFault fault, ref string faultAction);
}

更多详细信息请参见:http://www.extremeexperts.com/Net/Articles/ExceptionHandlingInWCF.aspx

You should use the build in WCF tracing and logging. Then you can use the WCF Trace Viewer which will piece together logs from service and client so that you can see the full message flow.

http://www.devx.com/dotnet/Article/37389/0/page/6

There is the IErrorHandler interface in WCF:

public interface IErrorHandler
{
    bool HandleError(Exception error, MessageFault fault);
    void ProvideFault(Exception error, ref MessageFault fault, ref string faultAction);
}

More details here: http://www.extremeexperts.com/Net/Articles/ExceptionHandlingInWCF.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文