views:

71

answers:

5

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.

+4  A: 

If 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.

consultutah
A: 

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.

Ben Robinson
A: 

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.

treefrog
can I do this without messing with the web.config file?
user279521
I get something incomprehensive.... methodName = "System.Object CallMethod(System.Object, System.String, System.Object[])"
user279521
Yeah you don't have to mess with the web.config file. Also, I forgot to add a property call to GetMethod(). It's fixed.
treefrog
A: 

Try:

string errorMessage = Exception.Message
string errorDetails = Error.Stacktracer
+2  A: 

Catch (Exception e)
{
string errMessage = e.Message
string errTraceDetails = e.StackTrace
}

This should give you the details you need.

LearningCSharp
wow. Its that simple !!
user279521