为什么 NLog 在 Controller.OnException 后不工作?
Why is NLog not working from Controller.OnException?
我已经正确设置了 NLog,我知道它正在读取配置文件。
我有一个测试控制器动作:
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);
}
我也登录了我的 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;
}
当我 运行 在 "DEV"/localhost 中安装我的系统时,这一切都完美无缺。
当我将 ASP.NET MVC 项目发布到我的测试服务器时,除 OnException 方法 中的日志记录外,所有日志记录都正常工作。 OnException 方法通过显示错误视图以其他方式工作。为什么?
我正在登录数据库,我可以在 DEV 和 TEST 中手动 运行 INSERT 语句,没有任何问题。我的测试系统部署到 IIS 虚拟目录。
至于我为什么使用OnException
,那是另一个问题。
这就是我遇到的问题。不要注册 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());
}
我已经正确设置了 NLog,我知道它正在读取配置文件。
我有一个测试控制器动作:
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);
}
我也登录了我的 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;
}
当我 运行 在 "DEV"/localhost 中安装我的系统时,这一切都完美无缺。
当我将 ASP.NET MVC 项目发布到我的测试服务器时,除 OnException 方法 中的日志记录外,所有日志记录都正常工作。 OnException 方法通过显示错误视图以其他方式工作。为什么?
我正在登录数据库,我可以在 DEV 和 TEST 中手动 运行 INSERT 语句,没有任何问题。我的测试系统部署到 IIS 虚拟目录。
至于我为什么使用OnException
,那是另一个问题。
这就是我遇到的问题。不要注册 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());
}