Search code examples
web-servicesunhandled-exception

Catching all unhandled exceptions in WCF web service in c#


I need to catch all unhandled exceptions in my web service.

My service is created with Visual Studio Express 2013 for Web and it is a WCF service style using c#.

I already do that in Android but I have no idea how to do it with the WCF service. Any ideas?


Solution

  • As far as I know it is not possible to do that in code as you can do in Android but you can enable tracing in the Web.config file this way:

    <?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>
    

    The switchValue="Error" can be changed depending on the level you want. More info: 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