简体   繁体   English

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

[英]MVC 5 c# reporting errors back to the database

we have an MVC application , what is the best way to record errors generated (by users) back to our support database? 我们有一个MVC应用程序,将(用户)产生的错误记录回我们的支持数据库的最佳方法是什么? users should only see "this error has been reported" error message 用户应该只看到“已报告此错误”错误消息

all the details should be written to to a database 所有详细信息都应写入数据库

please help! 请帮忙!

I suggest you either make use of External library like Log4Net or create you own library class which will do logging for you. 我建议您要么使用Log4Net之类的外部库,要么创建自己的库类来为您做日志记录。

One more suggestion is make use of ExceptionFilter which is avaiable in MVC file and write code of logging in that filter. 另一种建议是使用MVC文件中可用的ExceptionFilter并在该过滤器中编写登录代码。 I am suggesting becuase it will not cause you to write code again and again it follows DRY principle. 我建议这样做,因为它不会导致您一次又一次地遵循DRY原理编写代码。

Example : 范例:

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;  
        }  
    }  
}

than you can do like this 比你能做到的

//Over controller  
[CustomExceptionFilter]  
public class HomeController:Controller 
{  
   //......  
}  


//Over the Action  
[CustomExceptionFilter]  
public ActionResult Index() 
{  
   //.......  
}  

Check this for log4net : log4net Tutorial 检查此日志是否为log4net: log4net教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM