views:

110

answers:

2

Is it possible to log WCF service exceptions? I have added in the app.config. But still the exception soap message is missing in the wcf log file. All the remaining messages for which there is no exceptions can be seen in the WCF log file. Here is my code & app.config. Any pointers are highly appreciated.

public string GetName(int employeeId)
{
    string _fullName;

    try
    {
        switch (employeeId)
        {
            case 1:
                _fullName = "Dejan Dimitrovski";
                break;
            case 2:
                _fullName = "John Doe";
                break;
            case 3:
                _fullName = "Sue Marcus";
                break;
            case 4:
                throw new Exception("test exception");
            default:
                _fullName = "N/A";
                break;
        }
    }
    catch (Exception ex)
    {
        throw new FaultException(ex.Message, new FaultCode("Server"));   
    }
    return _fullName;
}

my app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
        propagateActivity="true">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelTraceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelMessageLoggingListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="app_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0,&#xD;&#xA;          Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
        <filter type="" />
      </add>
      <add initializeData="app_messages.svclog" 
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp" >
        <filter type="" />

      </add>
    </sharedListeners>
  </system.diagnostics>

  <system.serviceModel>
    <diagnostics>

      <messageLogging logEntireMessage="true"
                      logMalformedMessages="true"
                      logMessagesAtServiceLevel="true"
                      logMessagesAtTransportLevel="false"   />

    </diagnostics>
    <behaviors>
      <serviceBehaviors >
        <behavior name="EmployeeService"  >
          <serviceMetadata httpGetEnabled="true"   />
          <serviceDebug includeExceptionDetailInFaults="true"  />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings />
    <services>
      <service behaviorConfiguration="EmployeeService" name="SoftLab.Wcf.Service.EmployeeService">
        <endpoint name="basicHttpBinding"
                  address="basicEmployeeService"
                  binding="basicHttpBinding"
                  bindingNamespace="http://softlab.mkdot.net/binding/employee/2008/07"
                  contract="SoftLab.Wcf.Service.IEmployeeContract" />

        <endpoint name="mex"
                  address="mex"
                  binding="mexHttpBinding"
                  bindingConfiguration=""
                  contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>
A: 

I've been using log4net for a while now, and I have to say, all in all, even though it can be a pain at times, once its running it is a generally good all round logger.

It takes a while to set it up, and these instructions are out of the scope of the answer to this question. I highly recommend it for all round exception and info logging.

JL
-1: this is not an answer to this question.
John Saunders
+1  A: 

WCF logs all unhandled exceptions. You have handled the exception and returned a fault instead.

Try removing the try/catch block. I think your client will notice nearly the same message coming back, and you'll have your exceptions logged.

John Saunders
@John Saunders, Thanks for the suggestion. I removed my try catch, though client received the exception properly, the exception message is not logged. Only the request message is logged but not the response which contains exception is not logged. Could you please let me know what else I have to change in my config?
funwithcoding
@funwithcoding: are you logging on the client or on the server?
John Saunders
I am logging on server.
funwithcoding
@funwithcoding: then I don't know why you're not seeing exceptions. Just for fun, try changing the level to the lowest (Informational?)
John Saunders
@John Saunders, Still no luck. Could you please give me a working project or point to a working source code. I want to log all messages including exception messages. Thanks in advance
funwithcoding