2010-03-06 52 views
1

是否可以记录WCF服务异常?我已经在app.config中添加了。但仍然在wcf日志文件中丢失异常soap消息。在WCF日志文件中可以看到所有其余没有例外的消息。这里是我的代码& app.config。任何指针都非常感谢。WCF:如何记录异常?

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> 

回答

2

WCF记录所有未处理例外。你已经处理了异常,而是返回了一个错误。

尝试删除try/catch块。我认为你的客户会注意到几乎同样的消息回来,你会记录你的例外。

+0

@John Saunders,谢谢你的建议。我删除了我的try catch,尽管客户端正确接收到异常,但异常消息未被记录。只记录请求消息,但不记录包含异常的响应。你能不能让我知道还有什么我需要改变我的配置? – funwithcoding 2010-03-06 14:48:03

+0

@funwithcoding:你是登录在客户端还是服务器上? – 2010-03-06 15:52:07

+0

我正在登录服务器。 – funwithcoding 2010-03-06 16:15:09