Reputation: 25157
I have NLog set up correctly and I know it is reading the config file.
I have a test controller action:
public JsonResult Exception()
{
throw new Exception("This is a test exception thrown from TestsController. It should be logged and displayed in an error page from BaseController.OnException().");
}
public JsonResult Logging()
{
Log.Info("Logging test called from TestsController");
Log.Error("Logging test called from TestsController");
return Json("Called Log.Info and Log.Error.", JsonRequestBehavior.AllowGet);
}
I also have logging in my BaseController.
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled)
{
return;
}
var e = filterContext.Exception;
Log.Error(e, "BaseController.OnException caught exception: ");
var controllerName = (string)filterContext.RouteData.Values["controller"];
var actionName = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(e, controllerName, actionName);
filterContext.Result = new ViewResult
{
ViewName = "~/Views/Shared/Error.cshtml",
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
};
filterContext.ExceptionHandled = true;
}
This all works perfectly when I am running my system in "DEV"/localhost.
When I publish the ASP.NET MVC project to my test server, all of the logging works except for the logging in the OnException Method. The OnException method works otherwise by displaying the error view. Why?
I am logging to a DB and I can manually run the INSERT statement in DEV and TEST without any issues. My test system is deployed to an IIS virtual directory.
As to why I am using OnException
, that is another question.
Upvotes: 0
Views: 239
Reputation: 25157
This was the issue for me. Don't register the HandleErrorAttribute.
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
// Logging was not working. Remove HandleErrorAttribute. Allows OnException to work.
// filters.Add(new HandleErrorAttribute());
filters.Add(new UpstreamAuthorizeAttribute());
}
Upvotes: 1