[英]MVC 5 c# reporting errors back to the database
我們有一個MVC應用程序,將(用戶)產生的錯誤記錄回我們的支持數據庫的最佳方法是什么? 用戶應該只看到“已報告此錯誤”錯誤消息
所有詳細信息都應寫入數據庫
請幫忙!
我建議您要么使用Log4Net之類的外部庫,要么創建自己的庫類來為您做日志記錄。
另一種建議是使用MVC文件中可用的ExceptionFilter
並在該過濾器中編寫登錄代碼。 我建議這樣做,因為它不會導致您一次又一次地遵循DRY原理編寫代碼。
范例:
public class CustomExceptionFilter: FilterAttribute,
IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (!filterContext.ExceptionHandled)
{
//log error here
Logger.LogException(filterContext.Exception);
//this will redirect to error page and show use logging is done
filterContext.Result = new
RedirectResult("customErrorPage.html");
filterContext.ExceptionHandled = true;
}
}
}
比你能做到的
//Over controller
[CustomExceptionFilter]
public class HomeController:Controller
{
//......
}
//Over the Action
[CustomExceptionFilter]
public ActionResult Index()
{
//.......
}
檢查此日志是否為log4net: log4net教程
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.