
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Obtaining a Stack Trace
|
413
This regular expression captures every method call in the stack trace string. Note
that, if the correct symbols are located for your assembly, the stack trace might look
like this:
at Chapter_Code.Class1.ObtainingStackTrace( ) in c:\book cs cookbook\test.cs:line 260
at Chapter_Code.Class1.Main(String[] args) in c:\book cs cookbook\main.cs:line 78
However, if the correct symbols cannot be found, the stack trace string will look sim-
ilar to the following:
at Chapter_Code.Class1.ObtainingStackTrace( )
at Chapter_Code.Class1.Main(String[] args)
The file and line numbers are not displayed in this case, and the regular expression
must take this into account.
To get a count of the stack depth, use the
Count property of the MatchCollection
object to give the total number of method calls in the stack. In addition, you can
obtain each individual method call as an independent string by iterating through the
MatchCollection object. The code to do this is:
Console.WriteLine("-------------");
foreach(Match m in MethodCallMatches)
{
Console.WriteLine(m.Value + System.Environment.NewLine + "-------------");
}
This code will display the following:
-------------
at Chapter_Code.Class1.ObtainingStackTrace( ) in
c:\book cs cookbook\test.cs:line 260
-------------
at Chapter_Code.Class1.Main(String[] ...