Is it possible to extract the Class name and the Filename from an exception object?
I am looking to integrate better logging into my application, and I want to include detailed information of where the exception occurred.
In MVC the Stacktrace does not return the filename & class name, and I'm a bit lost as to where to look for these.
Thanks
You can create a StackTrace
object from an exception object. It will include the StackFrame
s that the exception has info on. You could then find the file and method names, positions and whatnot if they are available. Of course this should go without saying but all of this is available only if you compiled your assembly to include debugging symbols (which I assume could be available in MVC).
catch (Exception ex)
{
var st = new StackTrace(ex, true); // create the stack trace
var query = st.GetFrames() // get the frames
.Select(frame => new
{ // get the info
FileName = frame.GetFileName(),
LineNumber = frame.GetFileLineNumber(),
ColumnNumber = frame.GetFileColumnNumber(),
Method = frame.GetMethod(),
Class = frame.GetMethod().DeclaringType,
});
// log the information obtained from the query
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With