2010-03-06 94 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