C# – print a file name and line number when an exception was thrown

    public static class ExceptionUtils
    {
        public static void PrintException(Exception e)
        {
            var frame = new StackTrace(e, true).GetFrame(0);
            if (frame != null)
            {
                var str = string.Format("File: {0}({1}){2}{3}", frame.GetFileName(), frame.GetFileLineNumber(), Environment.NewLine, e.Message);
                Trace.WriteLine(str);
            }
            else
                Trace.WriteLine(e.Message);
        }
    }
Advertisements
This entry was posted in .NET, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s