Navigating in Exception Stack Traces in Visual C-Sharp

From eqqon

(Difference between revisions)
Jump to: navigation, search
m (Error Formatting for Visual C#)
m
 
(4 intermediate revisions not shown)
Line 1: Line 1:
[[Category:CSharp]]
[[Category:CSharp]]
 +
<div style="margin-left:100px; margin-right:200px;">
= Navigating in Exception Stack Traces in Visual C# =
= Navigating in Exception Stack Traces in Visual C# =
Today I wrote a nice little debug helper function called '''PrintException'''. It prints the relevant informations about an exception and its inner exceptions to the output recursively. The clue is, that it does this in a format that is understood by the '''error parser of Microsoft Visual Studio'''. So if you get an exception you can '''easily navigate''' to the source locations by clicking on the lines in the printed stacktrace. It also prints inner exceptions recursively with increasing indent for better readability.  
Today I wrote a nice little debug helper function called '''PrintException'''. It prints the relevant informations about an exception and its inner exceptions to the output recursively. The clue is, that it does this in a format that is understood by the '''error parser of Microsoft Visual Studio'''. So if you get an exception you can '''easily navigate''' to the source locations by clicking on the lines in the printed stacktrace. It also prints inner exceptions recursively with increasing indent for better readability.  
Line 6: Line 7:
Note: Error parser formatting works, of course, only when the exception is raised in an assembly that has been compiled in debug mode. Also the redirection of standard out to Visual Studio's output window is only activated by default for a Windows Forms application not for a Console application.
Note: Error parser formatting works, of course, only when the exception is raised in an assembly that has been compiled in debug mode. Also the redirection of standard out to Visual Studio's output window is only activated by default for a Windows Forms application not for a Console application.
-
Here is a simple example of usage of '''Debug.PrintException''':
+
== A Simple Example of Usage ==
-
 
+
<br>
<span><span class="S5">public</span><span class="S0"> </span><span class="S5">static</span><span class="S0"> </span><span class="S5">class</span><span class="S0"> </span>PrintExceptionTest<br />
<span><span class="S5">public</span><span class="S0"> </span><span class="S5">static</span><span class="S0"> </span><span class="S5">class</span><span class="S0"> </span>PrintExceptionTest<br />
<span class="S10">{</span><br />
<span class="S10">{</span><br />
Line 28: Line 29:
<span class="S0">&nbsp; &nbsp; </span><span class="S10">}</span><br />
<span class="S0">&nbsp; &nbsp; </span><span class="S10">}</span><br />
<span class="S10">}</span></span>
<span class="S10">}</span></span>
 +
<br>
The output generated by the above example follows. Links to source files are recognized by Visual Studio's error parser. Works also in some other IDEs and editors like Scite:
The output generated by the above example follows. Links to source files are recognized by Visual Studio's error parser. Works also in some other IDEs and editors like Scite:
Line 48: Line 50:
-
And this is the implementation of PrintException:
+
== The Implementation of PrintException ==
 +
<br>
<span><span class="S0"></span><br />
<span><span class="S0"></span><br />
<span class="S5">using</span><span class="S0"> </span>System<span class="S10">;</span><br />
<span class="S5">using</span><span class="S0"> </span>System<span class="S10">;</span><br />
Line 90: Line 93:
<span class="S10">}</span><br />
<span class="S10">}</span><br />
<span class="S0"></span></span>
<span class="S0"></span></span>
-
 
+
<br>
Blogged by --[[User:Henon|Henon]] 12:07, 9 November 2007 (CET)
Blogged by --[[User:Henon|Henon]] 12:07, 9 November 2007 (CET)
 +
 +
 +
This article has also been posted on [http://www.codeproject.com/useritems/NavigatingExceptions.asp The Code Project]
 +
</div>

Latest revision as of 11:05, 22 November 2007


Navigating in Exception Stack Traces in Visual C#

Today I wrote a nice little debug helper function called PrintException. It prints the relevant informations about an exception and its inner exceptions to the output recursively. The clue is, that it does this in a format that is understood by the error parser of Microsoft Visual Studio. So if you get an exception you can easily navigate to the source locations by clicking on the lines in the printed stacktrace. It also prints inner exceptions recursively with increasing indent for better readability.

Note: Error parser formatting works, of course, only when the exception is raised in an assembly that has been compiled in debug mode. Also the redirection of standard out to Visual Studio's output window is only activated by default for a Windows Forms application not for a Console application.

A Simple Example of Usage


public static class PrintExceptionTest
{
    static void test()
    {
        try { test1(); }
        catch (Exception e) { throw new Exception("outer exception message", e); }
    }

    static void test1()
    {
        throw new InvalidOperationException("inner exception message");
    }

    public static void Main()
    {
        try { test(); }
        catch (Exception e) { Debug.PrintException(e); }
        Console.ReadLine();
    }
}

The output generated by the above example follows. Links to source files are recognized by Visual Studio's error parser. Works also in some other IDEs and editors like Scite:

********************************************************************************
Exception: "outer exception message"
--------------------------------------------------------------------------------
InnerException:
   ********************************************************************************
   InvalidOperationException: "inner exception message"
   --------------------------------------------------------------------------------
     c:\C#\snippets\print_exception.cs(58,1):   PrintExceptionTest.test1()
     c:\C#\snippets\print_exception.cs(48,1):   PrintExceptionTest.test()
   ********************************************************************************
  c:\C#\snippets\print_exception.cs(52,1):   PrintExceptionTest.test()
  c:\C#\snippets\print_exception.cs(65,1):   PrintExceptionTest.Main()
********************************************************************************


The Implementation of PrintException



using System;

public static class Debug
{
    public static void PrintException(Exception exception)
    {
        PrintException(exception, "");
    }

    public static void PrintException(Exception exception, string indent)
    {
        string stars = new string('*', 80);
        Console.WriteLine(indent + stars);
        Console.WriteLine(indent + "{0}: \"{1}\"", exception.GetType().Name, exception.Message);
        Console.WriteLine(indent + new string('-', 80));
        if (exception.InnerException != null)
        {
            Console.WriteLine(indent + "InnerException:");
            PrintException(exception.InnerException, indent + "   ");
        }
        foreach (string line in exception.StackTrace.Split(new string[] { " at " }, StringSplitOptions.RemoveEmptyEntries))
        {
            if (string.IsNullOrEmpty(line.Trim())) continue;
            string[] parts;
            parts = line.Trim().Split(new string[] { " in " }, StringSplitOptions.RemoveEmptyEntries);
            string class_info = parts[0];
            if (parts.Length == 2)
            {
                parts = parts[1].Trim().Split(new string[] { "line" }, StringSplitOptions.RemoveEmptyEntries);
                string src_file = parts[0];
                int line_nr = int.Parse(parts[1]);
                Console.WriteLine(indent + "  {0}({1},1):   {2}", src_file.TrimEnd(':'), line_nr, class_info);
            }
            else
                Console.WriteLine(indent + "  " + class_info);
        }
        Console.WriteLine(indent + stars);
    }
}

Blogged by --Henon 12:07, 9 November 2007 (CET)


This article has also been posted on The Code Project