Exception logging in ASP.NET MVC Application

2.1k views Asked by At

Currently I log exceptions as follows-

 public ActionResult Login()
 {
        try
        {
            throw new Exception("test");
        }
        catch (Exception ex)
        {
            LogException(ex);
        }  
        return View();
    }

I can get a clear & detail information about the exception from LogException() method here. See below-

Created on: 27-Dec-2016, 06.34.33 PM
Type: System.Exception
Error Description: test 
Source File: ...\Controllers\LoginController.cs
Method: Login
Line: 20
Column: 17

I tried the same method in -

public class MyCustomAttribute : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        Exception ex = filterContext.Exception;            
        LogException(ex)
    }
}

public class MyCustomAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
         Exception ex = filterContext.Exception;            
         LogException(ex)
    }
}

I also tried overloading OnException method-

public abstract class BaseController : Controller
{
        public BaseController() {}

        protected override void OnException(ExceptionContext filterContext)
        {
            Exception ex = filterContext.Exception;
            filterContext.ExceptionHandled = true;

            LogException(ex);
        }
}

In all three abose cases I am getting no information about Source File, Method, Line and Column-

 Created on: 27-Dec-2016, 06.44.45 PM
 Type: System.Exception
 Error Description: test 
 Source File: 
 Method: <BeginInvokeAction>b__1e
 Line: 0
 Column: 0

This is my method to log exception-

       public static void Create(Exception exception, String rootDirectoryPath)
        {
            try
            {
                StackTrace st = new StackTrace(exception, true);                 
                StackFrame frame = st.GetFrame(st.FrameCount - 1);
                string fileName = frame.GetFileName();
                string methodName = frame.GetMethod().Name;
                int line = frame.GetFileLineNumber();
                int col = frame.GetFileColumnNumber();

                //Other code .....
            }
            catch (Exception)
            {
                //do nothing.               
            }
        }

My question is, is it possible to retrieve Source File, Method, Line and Column information from those three cases?

I would not like to handle exceptions by writing try .. catch.. every time.

I have heard about ELMAH & Log4Net. But not sure whether those library able to supply my desired information from exceptions.

Sorry for the long question.

2

There are 2 answers

2
Davidson Sousa On

You are reinventing the wheel as ELMAH will do the job just fine. Did you give it a try? Just a Nuget package and some configuration to be fully set.

You can read more about it from Scot Hanselman or read the tutorial.

0
jgauffin On

I'm the author of Coderr which also takes care of tracking exceptions for you.

the problem with your solution is that the exception filter (and the ASP.NET framework calls to invoke it) will be part of the stack trace, thus getting frames from the stack trace object wont work.

Also note that if the .PDB file is not included when you put your web site in production you wont get file numbers.

The most common approach is just to log exception.ToString() which will include line numbers in the stack trace (if there is a PDB file).

Why do you want to use your custom approach?