Getting the Current Stack Trace

An entry about c# 3.0 Publication date 10. April 2007 23:41

Shaping up to be a busy busy week at work this (and many to come for that matter), but here's a short little nugget of coolness that I came across today while working on a custom wrapper class for the System.Diagnostic.Trace class. I'll probably write a longer post about that later actually, cause there's some really cool stuff you can do with  TraceListeners and its friends. Anyways, I wanted to append the (fully classified) name of the method that was responsible for logging any given message. Obviously, I could request that the caller sent its name as a string to my log method, but where's the finesse in that?

A bit of digging around, and I found just what I wanted - the StackTrace and StackFrame classes. Basically, they'll let you walk the current stack trace - you know, that gibberish exceptions scream at you whenever they get a chance, or Visual Studio lists in its Call Stack window when stepping through a program:

Visual Studio Call Stack

So then, to get the name of the method that called my log method (the 'caller'), I could just get the StackFrame one level up. Here's a simplistic example which just prints the log message to the console, appended by the name of the caller:

public static void Log(string message)
{
StackFrame frame = new StackFrame(1);
MethodBase method = frame.GetMethod();
message = String.Format("{0}.{1} : {2}",
method.DeclaringType.FullName, method.Name, message);
Console.WriteLine(message);
}

Pretty cool, yes? Okay, now for the bad news:

You can't really depend on this for anything other than Debug builds. Once you build your application in Release mode, the compiler may very well decide to optimize your code by inlining some of the methods. Additionally, any native calls (f.ex stuff in mscorelib) will not show up in the stack trace. And also, cross-threading will ruin it too. Mike Stall has a post detailing some of these issues, with an example of inlining.

So yeah, there's a few caveats here that basically renders this approach useless for anything but debugging. But then its quite cool, and you can also get alot of other information about the current frame - especially if you have the debug symbols loaded.

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Powered by BlogEngine.NET 1.4.5.0

Welcome!

My name is Fredrik Kalseth, and this is my blog - thanks for visiting! I am fortunate enough to work with what I love for a living, and this blog is essentially the biproduct of that.

I work as a senior consultant for Capgemini, and am also an active participant in the Norwegian .NET community, as an avid attendee but also as a speaker (most recently at NNUG and MSDN Live).

As a developer, I have a wide circle of interest. My primary passion is for agile, test-driven development, with focus on best practices and clean code. That said, I also love to work on the frontend, especially with web development.

On Twitter? My handle is fkalseth. On LinkedIn? I`m there too.


Disclaimer

This is a personal blog; any opinions expressed here are my own and do not necessarily reflect those of my employer. All content herein is my own original creation, and as such is protected by copyright law. Unless otherwise stated, all source code posted on this blog is freely usable under the Microsoft Permissive License.

What Readers Talk About

Comment RSS