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

标签 c# asp.net-mvc error-handling

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

相关文章:

sql-server - 从vb.net调用具有已知错误的存储过程的错误处理

php - Laravel 5.4在 View 中显示错误

c# - Notepad++ 的 Compare 插件算法

c# - 使用 Azure 数据工厂 Web 事件发布表单数据会向 C# 脚本返回不同的结果

c# - 绑定(bind)到一个模型属性的多个表单元素

c# - 用于搜索任何或部分字符串的 MVC ASP.Net LINQ 语法

c# - IServiceCollection 不包含 AddMvc() 的定义

c# - 高性能服务器 - 我应该使用什么?

c# - 使用 MVC 6 显示从 ASP.NET 5 中的 WebRequest 获取的 JSON 数据

python - 为什么会出现IndexError : list index out of range