Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8689249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:35:53+00:00 2026-06-12T23:35:53+00:00

Recently i’ve encountered a problem of getting as much details from exception as i

  • 0

Recently i’ve encountered a problem of getting as much details from exception as i possibly could. The reason? Well, when you need to solve problems in shipped product the log is usually the only thing you have.

Obviously

Exception.ToString()

works pretty well but it is not very helpful when you deal with FaultException and who knows what surprises can custom exceptions give you.

So what is the best way to get exception details with decent level of paranoia?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:35:55+00:00Added an answer on June 12, 2026 at 11:35 pm

    I’ve looked around and googled on the topic. Susrpisingly there are not so many discussions on this. Anyway I tried to compound quintessence here.

    Talk is cheap. Show me the code.

    1. Here we have the sample code throwing the exception.

      protected void TestExceptionDetails()
      {
          try
          {
              int zero = 0;
      
              try
              {
                  int z = zero / zero;
              }
              catch (Exception e)
              {
                  var applicationException = new ApplicationException("rethrow", e);
                  // put some hint why exception occured
                  applicationException.Data.Add("divider_value", zero);
                  throw applicationException;
              }
          }
          catch (Exception e)
          {
              var extendedexceptionDetails = GetExtendedexceptionDetails(e);
              log.ErrorFormat("Detailed:{0}", extendedexceptionDetails);
          }
      }
      
    2. Here is the method GetExtendedExceptionDetails:

      /// <summary>
      /// This utility method can be used for retrieving extra details from exception objects.
      /// </summary>
      /// <param name="e">Exception.</param>
      /// <param name="indent">Optional parameter. String used for text indent.</param>
      /// <returns>String with as much details was possible to get from exception.</returns>
      public static string GetExtendedexceptionDetails(object e, string indent = null)
      {
          // we want to be robust when dealing with errors logging
          try
          {
              var sb = new StringBuilder(indent);
              // it's good to know the type of exception
              sb.AppendLine("Type: " + e.GetType().FullName);
              // fetch instance level properties that we can read
              var props = e.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead);
      
              foreach (PropertyInfo p in props)
              {
                  try
                  {
                      var v = p.GetValue(e, null);
      
                      // in case of Fault contracts we'd like to know what Detail contains
                      if (e is FaultException && p.Name == "Detail")
                      {
                          sb.AppendLine(string.Format("{0}{1}:", indent, p.Name));
                          sb.AppendLine(GetExtendedexceptionDetails(v, "  " + indent));// recursive call
                      }
                      // Usually this is InnerException
                      else if (v is Exception)
                      {
                          sb.AppendLine(string.Format("{0}{1}:", indent, p.Name));
                          sb.AppendLine(GetExtendedexceptionDetails(v as Exception, "  " + indent));// recursive call
                      }
                      // some other property
                      else
                      {
                          sb.AppendLine(string.Format("{0}{1}: '{2}'", indent, p.Name, v));
      
                          // Usually this is Data property
                          if (v is IDictionary)
                          {
                              var d = v as IDictionary;
                              sb.AppendLine(string.Format("{0}{1}={2}", " " + indent, "count", d.Count));
                              foreach (DictionaryEntry kvp in d)
                              {
                                  sb.AppendLine(string.Format("{0}[{1}]:[{2}]", " " + indent, kvp.Key, kvp.Value));
                              }
                          }
                      }
                  }
                  catch (Exception exception)
                  {
                      //swallow or log
                  }
              }
      
              //remove redundant CR+LF in the end of buffer
              sb.Length = sb.Length - 2;
              return sb.ToString();
          }
          catch (Exception exception)
          {
              //log or swallow here
              return string.Empty;
          }
      }
      

    As you can see we use Reflection to get instance properties and then get their values. I know it is expensive but we don’t really know what possible properties the concrete exception exposes. And we all hope that errors won’t occur so often in application that it would kill the performance.

    Now let’s look at what we actually gained.

    This is what Exception.ToString returns:

    System.ApplicationException: rethrow ---> System.DivideByZeroException: Attempted to divide by zero.
       at NET4.TestClasses.Other.TestExceptionDetails() in c:\tmp\prj\NET4\TestClasses\Other.cs:line 1116
       --- End of inner exception stack trace ---
       at NET4.TestClasses.Other.TestExceptionDetails() in c:\tmp\prj\NET4\TestClasses\Other.cs:line 1123
    

    And this returns our new method:

    Type: System.ApplicationException
    Message: 'rethrow'
    Data: 'System.Collections.ListDictionaryInternal'
     count=1
     [divider_value]:[0]
    InnerException:
      Type: System.DivideByZeroException
      Message: 'Attempted to divide by zero.'
      Data: 'System.Collections.ListDictionaryInternal'
       count=0
      InnerException: ''
      TargetSite: 'Void TestExceptionDetails()'
      StackTrace: '   at NET4.TestClasses.Other.TestExceptionDetails() in c:\tmp\prj\NET4\TestClasses\Other.cs:line 1116'
      HelpLink: ''
      Source: 'NET4'
    TargetSite: 'Void TestExceptionDetails()'
    StackTrace: '   at NET4.TestClasses.Other.TestExceptionDetails() in c:\tmp\prj\NET4\TestClasses\Other.cs:line 1123'
    HelpLink: ''
    Source: 'NET4'
    

    We use log4net for logging and to reduce the performance overhead there is ILog.IsErrorEnabled property. I just check it before calling the extended exception handling.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently, I had the need for a function that I could use to guarantee
Recently, I was writing a class in which I discovered that I could reduce
Recently we started seeing a problem where the Application_Error event handler (for HttpApplication.Error )
Recently I am making a server-client program using multithread concept. For some reason, I
Recently, I'm trying to solve all the exercises in CLRS. but there are some
Recently two users of our software from the same company started experiencing random closures
Recently I discovered a problem on the midas and I fixed it, the problem
Recently I have encountered the concept of NoSQL and as far as I manage
Recently there was some upgrade happened from frame work 2.0 to 4.0, so after
Recently my records started to disappear from my application's database so I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.