title: How to use the inner exception when writing custom exception in c#
body: <p>I am writing custom exception class and using them to log to elmah whenever required.</p>
  
  <pre><code>public class MyCustomLogException : Exception
  {
      public string Property1 { get; protected set; }
      public string Property2 { get; protected set; }
      public string Property3 { get; protected set; }
  
      internal MyCustomLogException(string property1, string property2, string property3)
      {
          Property1 = property1;
          Property2 = property2;
          Property3 = property3;        
      }
  
      public override string Message
      {
          get
          {
              return string.Format("Error logging to db using Property1 = {0}, Property2 = {1} and Property3 = {2}", Property1, Property2, Property3);
          }
      }
  }
  </code></pre>
  
  <p>and I am using </p>
  
  <pre><code>public int LogSomethingToDb(SomeModel log)
  {
      try
      {
          // log to db
      }
      catch(Exception exception)
      {
          // to do how to use this exception ?
          throw new MyCustomLogException(log.Property1, log.Property2, log.Property3);
      }
  }
  </code></pre>
  
  <p>How do I use the above exception message, stack trace etc so that I dont end up eating the 'exception' message.</p>
  
  <p>I want the exact detail of the exception to also be logged for eg if its a entity framework exception or a null ref exception etc just by reading the elmah logs.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Yea sure earlier with my code i was getting MyCustomLogException as the exception logged and message of "Error logging to db using Property1 = {0}, Property2 = {1} and Property3 = {2}" as the exception message. This way just by reading the log I can figure out what is wrong and then may be read the inner exception to get more details. –  Yasser 8 mins ago    </p>
  
  <p>Now with the <a href="https://stackoverflow.com/a/26250551/1182982">new code</a> the actual exception class Sql in my case is getting logged and the exception message of "Violation of UNIQUE KEY constraint 'IX_abc_log'. Cannot insert duplicate key in object 'dbo.abc'. The statement has been terminated." is logged and my custom exception class and message are logged as inner exception</p>