I’ve been trying to catch exceptions in my WCF service and store them in a log file (I don’t want the client to get any specific error message at this point).
Having read a few tutorials online I’ve added some logging configuration to the <system.diagnostics> section of web.config:
<sources>
<source name="System.ServiceModel.MessageLogging" switchValue="Error">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelMessageLoggingListener">
<filter type="" />
</add>
</listeners>
</source>
<source name="System.ServiceModel" switchValue="Error" propagateActivity="true">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelTraceListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="C:\inetpub\wwwroot\myApp\logs\Web_messages.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
<add initializeData="C:\inetpub\wwwroot\myApp\logs\Web_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
In the event of an exception I’ve been tried using Debug.WriteLine and Trace.WriteLine but nothing is getting written to the tracefile.
I know IIS / WCF can write to the log file, if I set the switchValue for either of the sources to be Verbose I get plenty of information (too much to be of use), but I only want the exception code.
Try this,
consider the following class
Now add the following config section to your configuration file
Finally, in order to use it:
Note that you can add trace methods for information and warnings to the trace utility class just as I did with the trace error methods.
Hope this is what you are seeking for.
EDIT:
Your other code did not run because you did not specify a custom listener to the System.Diagnostics.Trace class. To do so add the following section into your config:
and try using your old code. Hope this will de-baffle you 😉