title: How to Get Exception Detail
body: <p>I have a WCF service where I've implemented a custom Service Fault. I am throwing an error based a specific condition, so in an if statement I throw the error below.  </p>
  
  <pre><code>throw new FaultException&lt;CustomServiceFault&gt;(
      new CustomServiceFault
      {
          ErrorMessage = string.Format(
              "Error in response, code:{0} message:{1}", 
               response.Error.Code, 
               response.Error.Message),
          Source = "ConnectToOpenLdap Method",
          StackTrace = string.Format(
              "Error in response, code:{0} message:{1}", 
              response.Error.Code, 
              response.Error.Message)                                     
      },
      new FaultReason(
          string.Format(CultureInfo.InvariantCulture, "{0}", "Service fault exception")));
  </code></pre>
  
  <p>In the catch I rethrow the exception like this:</p>
  
  <pre><code>catch (Exception exception)
  {
      var customServiceFault = GetCustomException(exception);
      throw new FaultException&lt;CustomServiceFault&gt;(
          customServiceFault, 
          new FaultReason(customServiceFault.ErrorMessage), 
          new FaultCode("Sender"));              
  }   
  </code></pre>
  
  <p>The GetCustomException() method simply converts the exception to my custom exception object.  </p>
  
  <p>The problem is exception that gets passed to GetCustomException() does not have the details in the InnerException property.  Screenshot of what I see:</p>
  
  <p><a href="https://i.stack.imgur.com/EaFGx.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EaFGx.jpg" alt="enter image description here"></a></p>
  
  <p>How do I extract or get the custom ErrorMessage, Source, etc I set when throwing the exception in the if condition?  As you can see in the screenshot expanding "exception" shows the object type (I believe) and inside of that "Detail" shows the ErrorMessage, InnerExceptionMesage, Source, and StackTrace.  This is what I am after.  How do I extract these values in the GetCustomException() method?</p>
  
  <p>This is the GetCustomException() method:</p>
  
  <pre><code>private static CustomServiceFault GetCustomException(Exception exception)
  {                       
      var customServiceFault = new CustomServiceFault
      {
          ErrorMessage = exception.Message,
          Source = exception.Source,
          StackTrace = exception.StackTrace,
          Target = exception.TargetSite.ToString()
      };    
  
      return customServiceFault;
  }
  </code></pre>
  
  <p>CustomServiceFault class:</p>
  
  <pre><code>[DataContract]
  public class CustomServiceFault
  {
      [DataMember]
      public string ErrorMessage { get; set; }
      [DataMember]
      public string StackTrace { get; set; }
      [DataMember]
      public string Target { get; set; }
      [DataMember]
      public string Source { get; set; }
      [DataMember]
      public string InnerExceptionMessage { get; set; }
  }
  </code></pre>