Jobert Enamno
Jobert Enamno

Reputation: 4461

Get Class Name and Method Name Where Exception Occurred From Exception Details

I have this Application_Error event in my Global.asax.cs which will get any unhandled exceptions occurred in my application. I was able to get the error message. What I'm trying to get also is the Class name and Method name where exception happened so that it,s easier to trace where and what is the cause of the exception. Please help.

Global.asax.cs

protected void Application_Error(object sender, EventArgs e)
    {
        var errMsg = Server.GetLastError().Message;
        if (string.IsNullOrWhiteSpace(errMsg)) return;
        //Get Class name and method name
        //ErrorLog.SaveLog(errMsg, Classname, Methodname);
        Context.ClearError();
    }

Upvotes: 5

Views: 5331

Answers (2)

to StackOverflow
to StackOverflow

Reputation: 124696

The easiest thing to do is to log:

Server.GetLastError().ToString()

This includes the error message and stack trace for the exception and any inner exceptions, and in some cases, other useful information (*).

Note that Server.GetLastError().StackTrace will only give you the stack trace of the outer exception, less useful.

(*) By "other useful information", I mean that any Exception-derived class can override ToString to add additional information specific to that exception type. For example, SqlException overrides ToString to append the ClientConnectionId.

Upvotes: 2

L-Four
L-Four

Reputation: 13531

You can find this information in the call stack, using StackTrace, see the example at http://msdn.microsoft.com/en-us/library/system.diagnostics.stackframe.aspx.

Upvotes: 3

Related Questions