Using the NLogMvcHandlEerrorAttribute - MikeHanson/NLog.Mvc GitHub Wiki
NLog.Mvc includes a custom attribute derived from HandleErrorAttribute. NLogMvcHandleErrorAttribute extends the base functionality to log exceptions using the Logger included with NLog.Mvc. It can be used anywhere the base HandleErrorAttribute can be used so all documentation for this attribute applies.
I recommend you setup NLogMvcHandleErrorAttribute as a global filter so that it intercepts and logs all unhandled exceptions. The following example is taken from Bootstrapper.cs
included in the NLog.Mvc.4.Web project source, this file contains code that is very similar to the default contents of Global.asax.cs
in a new MVC project.
The default implementation of RegisterGlobalFilters
looks like this
private static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
After registering an instance of the NLog.Mvc.Logger as a singleton with AutoFac I modified the method like this
private static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
var logger = DependencyResolver.Current.GetService<ILogger>();
filters.Add(new NLogMvcHandleErrorAttribute(logger));
}
The key is that an instance of NLogMvcHandleErrorAttribute is added to the collection of global filters so that it can intercept unhandled exceptions and log them.