C# 在 WCF 中记录错误的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1927975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:55:56  来源:igfitidea点击:

Best way to log errors in WCF

c#.netwcflogging

提问by Kye

What's the best way to catch and log errors when developing a WCF service layer, and why?

开发 WCF 服务层时捕获和记录错误的最佳方法是什么,为什么?

I can think of three ways,

我能想到三个办法

1) Manual try/catches around each method.

1)手动尝试/捕获每种方法。

2) Leave the responsibility to the WCF engine.

2) 将责任留给 WCF 引擎。

3) Use a third party library such as Enterprise Library Policy Injection/Logging.

3) 使用第三方库,例如 Enterprise Library Policy Injection/Logging。

采纳答案by fspirit

I would implement custom IErrorHandlerand use log4net

我会实现自定义IErrorHandler并使用 log4net

[AttributeUsage (AttributeTargets.Interface)]
public class ErrorPolicyBehaviorAttribute : Attribute, IContractBehavior, IErrorHandler
    {
    private ILog m_logger;

    #region IErrorHandler

    public void ProvideFault (Exception error, MessageVersion version, ref Message fault)
        {
        return;
        }

    public bool HandleError (Exception error)
        {
        m_logger.Error (error.Message, error);
        return true;
        }

    #endregion

    #region IContractBehavior

    public void ApplyDispatchBehavior (ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        {
        ...init logger
        ......Add this class to a list of dispatchRuntime.ChannelDispatcher.ErrorHandlers...
        }

    #endregion
    }

This class also implements IContractBehavior, so you can use it as Attribute on your service contracts.

此类还实现了 IContractBehavior,因此您可以将其用作服务合同上的 Attribute。

[ErrorPolicyBehavior]
public interface IYourServiceContract
{ }

log4net is quite flexible, so you can log what you need and when you need.

log4net 非常灵活,因此您可以在需要时记录所需内容。

回答by Joshua Cauble

I would go with number 1. Mainly because of reduced overhead over number 3 and number 2 should just be a no-no.

我会选择 1。主要是因为减少了 3 号的开销,而 2 号应该只是一个禁忌。

Granted you still want to log to something say like a file or event manager. But I personnally would use log4net for it since it's a bit lighter than all the entlib stuff.

当然,您仍然想登录到诸如文件或事件管理器之类的东西。但我个人会使用 log4net,因为它比所有 entlib 的东西都要轻一些。

回答by t0mm13b

It might be worth your while to check out log4net. There is a good tutorial here on CodeProject.

检查log4net可能值得您花时间。CodeProject上有一个很好的教程here 。

回答by Daniel Vassallo

WCF can be configured to output traces for process milestones across all components of the applications, such as operation calls, code exceptions, warnings and other significant processing events.

WCF 可以配置为跨应用程序的所有组件输出流程里程碑的跟踪,例如操作调用、代码异常、警告和其他重要的处理事件。

The following is an app.configexample to enable tracing.

以下是app.config启用跟踪的示例。

<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Warning" propagateActivity="true" >
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>

      <source name="myUserTraceSource" switchValue="Warning, ActivityTracing">
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
    </sources>

    <sharedListeners>
      <add name="xml" 
           type="System.Diagnostics.XmlWriterTraceListener" 
           initializeData="TraceLog.svclog" />
    </sharedListeners>

  </system.diagnostics>
</configuration>

You can read more about WCF Tracing from MSDN: Configuring Tracing.

您可以从MSDN:配置跟踪中阅读有关 WCF 跟踪的更多信息。

Microsoft provides a Service Trace Viewer Toolto read .svclog files.

Microsoft 提供了一个服务跟踪查看器工具来读取 .svclog 文件。

Apart from tracing, you may also want to consider using log4netfor in-application logging.

除了跟踪之外,您可能还需要考虑使用log4net进行应用程序内日志记录。

回答by ram

If you are asking for logging framework ELMAHis also a good option to consider. If you dont like to litter your code with try/catch around each method, you can try using AOP frameworkswhich would give you the ability to handle exceptions by marking the method with Attributes

如果您要求使用日志记录框架ELMAH也是一个不错的选择。如果您不喜欢在每个方法周围使用 try/catch 来乱扔代码,您可以尝试使用AOP 框架,这将使您能够通过使用属性标记方法来处理异常