Asp.Net MVC中记录错误日志保存到本地txt文件
2019-05-17 17:08
1291 查看
为了方便查询系统出错弄个错误日志出来对于维护运维来说是很有必要的。
1、在Asp.Net MVC项目中的App_Start添加一个用于处理异常类的文件ErrorLog让他继承HandleErrorAttribute类并重写OnException方法
public class ErrorLog: HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { if(!filterContext.ExceptionHandled) { //当前Controller名称 string controllName = (string)filterContext.RouteData.Values["controller"]; //当前Action string actionName = (string)filterContext.RouteData.Values["action"]; //定义一个HandErrorInfo,用于Error视图展示异常信息 HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllName, actionName); //时间用来给txt命名 string thisTime = DateTime.Now.ToString("yyyyMMdd"); string errorDetails = $"出错时间:{DateTime.Now.ToString()},错误发生在{model.ControllerName}控制器的{model.ActionName},错误类型:{model.Exception.Message}"; string splitLine = "============================下一条=============================="; //日志存放位置,在项目目录里面一个月一个文件夹,一天一个文件 string path = HttpContext.Current.Server.MapPath(@"\ErrorLog\" + DateTime.Now.Year.ToString()+ @"\" + DateTime.Now.ToString("MM") + @"\" ); //判断文件夹不存在就创建 if (!Directory.Exists(path)) Directory.CreateDirectory(path); //写入日志 using (System.IO.StreamWriter file = new System.IO.StreamWriter(path + thisTime+".txt", true)) { file.WriteLine(errorDetails); file.WriteLine(model.Exception.StackTrace); file.WriteLine(splitLine); } //出错跳转到指定页面,如果Global.asax写了Application_Error方法可以不用写 ViewResult result = new ViewResult { ViewName = this.View,//设置异常时跳转的404页面 ViewData = new ViewDataDictionary<HandleErrorInfo>(model) //定义ViewData,泛型 }; filterContext.Result = result; filterContext.ExceptionHandled = true;//设置异常已处理 } } }
在视图里面的shared文件夹下面加一个Error视图,里面就是错误日志,类似于404页面一样的功能
2、在App_Start文件夹下面的FilterConfig.cs文件里面配置
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new ErrorLog() { View="Error"}); } }
ErrorLog别写错了,里面有个原始的HandleErrorAttribute类改名为你第一步添加的类
3、最后看一下Gloabl.asax里面注册了FilterConfig没有,一般都是有的
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } }
4、调用实例,我写的一个图片上传,没有文件夹抛异常
/// <summary> /// 图片上传 /// </summary> /// <param name="file">图片</param> /// <returns></returns> public ActionResult FileUpLoad1(HttpPostedFileBase file) { var ret = string.Empty; try { string fileName = Guid.NewGuid()+file.FileName; string filePath = Server.MapPath(@"\FileUp2\"); //if (!Directory.Exists(filePath)) // Directory.CreateDirectory(filePath); file.SaveAs(Path.Combine(filePath, fileName)); } catch (Exception ex) { ret = ex.Message + ":" + ex.InnerException; throw new Exception (ex.Message + ":" + ex.InnerException); } if(string.IsNullOrEmpty(ret)) return Json(new { code = "0", msg = "文件上传成功!", data = "" }); else return Json(new { code = "1", msg = "文件上传失败!", data = ret }); }
注意要想记录日志一定要把异常抛出来 就是 throw new Exception (ex.Message + ":" + ex.InnerException);
5、下面的效果
相关文章推荐
- WinForm、ASP.NET、MVC记录全局错误日志
- asp.net mvc中加入log4net记录错误日志
- asp.net 错误信息记录到日志文件
- asp.net MVC日志插件Log4Net学习笔记一:保存日志到本地
- Log4net 框架系列:log4net日志文件在asp.net中的应用实例-记录系统错误
- Log4net 框架系列:log4net日志文件在asp.net中的应用实例-记录系统错误
- Log4net 框架系列:log4net日志文件在asp.net中的应用实例-记录系统错误
- asp.net mvc中读取input file上传的txt文件内容,但不需要把文件保存到服务器上
- ASP.NET MVC中错误日志信息记录
- ASP.NET MVC利用log4net记录错误日志信息的简单运用
- asp.net mvc中加入log4net记录错误日志
- ELMAH(ASP.NET错误日志记录与通知)系列文章-概念篇
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库 自定义日志记录功能,按日记录,很方便 C#常量和字段以及各种方法的语法总结 类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法 asp.net webapi 自定义身份验证
- 使用Global.asax在ASP.NET中记录错误日志
- 将错误日志抛向后台以-.txt文件保存
- asp.net MVC日志插件Log4Net学习笔记二:保存日志到sqlserver的配置
- ElMAH(ASP.NET错误日志记录与通知)
- asp.net中当服务器出错时显示指定的错误页面,同时把错误信息写入系统日志文件的探讨
- ElMAH(ASP.NET错误日志记录与通知)系列文章-基础应用篇
- 在ASP.NET MVC中使用Log4Net进行多种HttpCode日志的记录