gpt4 book ai didi

c# - MVC 5 C#将错误报告回数据库

转载 作者:行者123 更新时间:2023-12-03 07:54:10 24 4
gpt4 key购买 nike

我们有一个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 Tutorial

关于c# - MVC 5 C#将错误报告回数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31380373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com