-2

I want to be able to print a stack trace in c# to a txt file as stated below. Note The Following :

  1. I know the Ex.Message wont get the stack trace. I want to use the FileHandler just to write the StackTrace Message into the Text File.
  2. The FileHandler Class internal code is not important. I want to know how to obtain the stacktrace on the Exception being catched here.

    try
    
    {
    
    //some code
    
    }
    
    // I just inherit from exception class (the message member, nothing big here)
    
    catch (CustomException ex) 
    {
    
    //I want to write the stacktrace here instead of the ex.Message
    
    FileHandler.Write("DeveloperErrors.txt", "Stack Trace" + ex.Message, System.IO.FileMode.Append);
    }
    
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Desmond Smith
  • 1,197
  • 1
  • 9
  • 13
  • 2
    -1 for _extreme_ lack of research effort. – SLaks Sep 30 '13 at 22:06
  • 1
    Consider using an actual logging system. – SLaks Sep 30 '13 at 22:07
  • Which is why i posted the answer founded in the sources below. But these do not indicate getting the current stacktrace of the exception being catched at the moment the program breaks. Ex.StackTrace seemed closest – Desmond Smith Sep 30 '13 at 22:18
  • You don't _want_ the current stack trace; you want the stack trace where the error happened. – SLaks Oct 01 '13 at 00:51

2 Answers2

8

You're looking for the dark secrets of the ex.StackTrace property.

EDIT: However, you should actually call ex.ToString() instead; this will include the message, stack trace, and the InnerException.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
-2

string tracktrace = System.Environment.StackTrace;

Community
  • 1
  • 1
Desmond Smith
  • 1,197
  • 1
  • 9
  • 13