WCF:如何记录异常?

时间:2010-03-06 03:04:22

标签: .net wcf silverlight

是否可以记录WCF服务异常?我在app.config中添加了。但是仍然在wcf日志文件中缺少异常soap消息。在WCF日志文件中可以看到没有例外的所有剩余消息。这是我的代码&的app.config。任何指针都非常受欢迎。

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;
}

我的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>

1 个答案:

答案 0 :(得分:2)

WCF记录所有未处理的异常。您已经处理了异常并返回了错误。

尝试删除try / catch块。我认为您的客户会注意到几乎相同的消息回来了,您将记录您的例外情况。