Skip to Main Content
C# Cookbook, 2nd Edition
book

C# Cookbook, 2nd Edition

by Jay Hilyard, Stephen Teilhet
January 2006
Intermediate to advanced content levelIntermediate to advanced
1184 pages
43h 23m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook, 2nd Edition
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Obtaining a Stack Trace
|
411
See Also
See Recipe 17.10; see the “Using User-Defined Exceptions” and “Exception Class”
topics in the MSDN documentation.
7.14 Obtaining a Stack Trace
Problem
You need a view of what the stack looks like at any particular point in your applica-
tion. However, you do not have an exception object from which to obtain this stack
trace.
Solution
Use the following line of code to obtain a stack trace at any point in your application:
string currentStackTrace = System.Environment.StackTrace;
The variable currentStackTrace now contains the stack trace at the location where
this line of code was executed.
Discussion
A good use of the Solution is tracking down stack overflow problems. You can
obtain the current stack trace at various points in your application and then calcu-
late the stack depth. This depth calculation can then be logged to determine when
and why the stack is overflowing or potential trouble spots where the stack may
grow very large.
It is very easy to obtain a stack trace using the
System.Environment.StackTrace prop-
erty. Unfortunately, this stack trace also lists three methods defined in the
System.
Environment
class that are called when you use the Environment.StackTrace property.
The returned stack trace, using this method, will look something like following: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook

C# Cookbook

Stephen Teilhet, Jay Hilyard
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 0596100639Supplemental ContentCatalog PageErrata