How to Get a Stack Trace from C# without throwing Exception

Say you have some logging in your code that finds something unusual going on.  In my case, I have a DataContext that I check to make sure it’s not already open before I open it.  The method that I call the DataContext in is a utility method that is buried many layers down.  When this problem is found, I don’t want to throw an exception, but I do want to log where I was.  It does not help me to know that I’m in my utility method.  I need to know the stack.

Below is some simple code that lets me do this.  Notice, it’s important to call StackTrace with true, otherwise the line numbers don’t appear.

HTH’s!

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TestClass.GoNow();
        }
    }

    class TestClass
    {
        public static void GoNow()
        {
            var stackTrace = new StackTrace(true);
            foreach (var r in stackTrace.GetFrames())
            {
                Console.WriteLine("Filename: {0} Method: {1} Line: {2} Column: {3}  ",
                    r.GetFileName(),r.GetMethod(), r.GetFileLineNumber(),
                    r.GetFileColumnNumber() );
            }
        }
    }


}
About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP since 2007. In addition, he's a multi-course video author at Pluralsight. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Comments

  1. How could We have missed this blogging site! You’ll find it amazing. Your structure is flawless, like you realize just how to proceed to accomplish make folks flock on your web page! I also much like the point of view you introduced to this matter. It really is like you’ve an insight that almost all individuals have not viewed previously. So remarkable to learn a web site similar to this.

  2. Michael Blake says:

    Just for info, this might break when you run de-attached from a debugger in Release mode as the StackTrace changes.

  3. Nice work but its probably easier to use Environment.StackTrace

  4. Stephen Disque says:

    Hello, i think that i saw you visited my website so i came to “return the favor”.I’m attempting to find things to improve my web site!I suppose its ok to use some of your ideas!!

  5. First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the

    information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful for community.

    There is a good C# resource site, Have alook

    http://CSharpTalk.com

    Thanks again
    Rahul

Follow

Get every new post delivered to your Inbox

Join other followers: