I am writing an error log procedure (for security reasons I cannot use log4net / elmah etc), and I wanted to know if it is possible to get the line number of the line where the error occured and / or the procedure name where the error occured?
I will be creating try catch finally statement blocks, so I am hoping to get the line number of the errorhandler (or the line that caused the error) and the related procedure name.
views:
71answers:
5If you have deployed the .pdb files as well, you should get the line number in the exception text.
I recommend against writing this yourself though. I use elmah for this type of thing everywhere.
Well the stack trace will be in the exception you catch and that will contain the method where the exception was thrown and the call tree right up to that exception. The line number is dependent on the debugging symbols (pdb files) but if they are deployed that will be in the stack trace as well.
Coincidentally I needed similar functionality not too long ago. This is what I found.
try
{
throw new Exception("Error Message.");
}
catch (Exception ex)
{
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
string fileLineNumber = trace.GetFrame(0).GetFileLineNumber().ToString();
string methodName = trace.GetFrame(0).GetMethod().Name;
}
For a full list of StackTrace methods, go here: Link.
Catch (Exception e)
{
string errMessage = e.Message
string errTraceDetails = e.StackTrace
}
This should give you the details you need.