在 C# 中捕获 WCF Web 服务中所有未处理的异常

Catching all unhandled exceptions in WCF web service in c#

我需要捕获我的网络服务中所有未处理的异常。

我的服务是使用 Visual Studio Express 2013 for Web 创建的,它是一种使用 C# 的 WCF 服务样式。

我已经在 Android 中做到了,但我不知道如何使用 WCF 服务来做到这一点。 有什么想法吗?

据我所知,无法像在 Android 中那样在代码中执行此操作,但您可以通过以下方式在 Web.config 文件中启用跟踪:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

       ...your other settings...

       <!-- Trace: -->
       <system.diagnostics>
          <sources>
                <source name="System.ServiceModel" 
                        switchValue="Error"
                        propagateActivity="true">
                <listeners>
                   <add name="traceListener" 
                       type="System.Diagnostics.XmlWriterTraceListener" 
                       initializeData= "C:\Logs\MyTraces.svclog" />
                </listeners>
             </source>
          </sources>
       </system.diagnostics>       

</configuration>

switchValue="Error"可以根据你想要的级别来改变。 更多信息: http://www.topwcftutorials.net/2012/06/4-simple-steps-to-enable-tracing-in-wcf.html https://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx