代码之家  ›  专栏  ›  技术社区  ›  funwithcoding

wcf:如何记录异常?

  •  1
  • funwithcoding  · 技术社区  · 14 年前

    是否可以记录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 回复  |  直到 14 年前
        1
  •  2
  •   John Saunders    14 年前

    WCF日志全部 未处理的 例外情况。您已经处理了异常并返回了一个错误。

    尝试删除try/catch块。我想你的客户会注意到几乎相同的消息,你会有你的异常记录。