开发者

WCF: How to log exceptions?

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开发者_如何学Python";
                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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜