net错误日志统一处理 Global.asax

webform错误日志统一处理 Global.asax

        protected void Application_Error(object sender, EventArgs e)
        {
            //在出现未处理的错误时运行的代码
            //在出现未处理的错误时运行的代码
            Exception objErr = Server.GetLastError().GetBaseException();
            string error = string.Empty;
            string errortime = string.Empty;
            string erroraddr = string.Empty;
            string errorinfo = string.Empty;
            string errorsource = string.Empty;
            string errortrace = string.Empty;
            error += "发生时间:" + System.DateTime.Now.ToString() + "
"
; errortime = "发生时间:" + System.DateTime.Now.ToString(); error += "发生异常页: " + Request.Url.ToString() + "
"
; erroraddr = "发生异常页: " + Request.Url.ToString() + "?action=" + (Request["action"] ?? ""); error += "异常信息: " + objErr.Message + "
"
; errorinfo = "异常信息: " + objErr.Message; errorsource = "错误帮助信息:" + objErr.HelpLink; errortrace = "堆栈信息:" + objErr.StackTrace; error += "--------------------------------------
"
; Server.ClearError(); Application["error"] = error; //独占方式,因为文件只能由一个进程写入. System.IO.StreamWriter writer = null; try { lock (this) { // 写入日志 string year = DateTime.Now.Year.ToString(); string month = DateTime.Now.Month.ToString(); string path = string.Empty; string filename = DateTime.Now.Day.ToString() + ".html"; path = Server.MapPath("~/ErrorLog1872/") + year + "/" + month; //如果目录不存在则创建 if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } System.IO.FileInfo file = new System.IO.FileInfo(path + "/" + filename); //文件不存在就创建,true表示追加 writer = new System.IO.StreamWriter(file.FullName, true); string ip = "用户IP:" + Request.UserHostAddress; string line = "-----------------------------------------------------"; string log = "


" + line + "
"
+ errortime + "  " + erroraddr + "
" + "
"
+ ip + errorinfo + "
"
+ errorsource + "
"
+ errortrace.Replace("\r\n", "
"
) + "

"
; writer.WriteLine(log); } } finally { if (writer != null) writer.Close(); } Response.Write("{\"ret\":-1}"); Response.End(); }

MVC错误信息统一处理
首先在App_Start里面新建一个类MyExceptionFilterAttribute.cs,继承HandleErrorAttribute,重写OnException方法。
修改App_Start/FilterConfig.cs

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            //filters.Add(new HandleErrorAttribute());
            filters.Add(new MyExceptionFilterAttribute());  
        }
public class MyExceptionFilterAttribute : HandleErrorAttribute 
    {
        public override void OnException(ExceptionContext filterContext)
        {
            var dt = DateTime.Now;
            var logPath = HttpContext.Current.Server.MapPath("/errorlog/" + dt.ToString("yyyy-MM"));
            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }
            var logFilePath = string.Format("{0}/{1}.txt", logPath, dt.ToString("yyyy-MM-dd"));
            StreamWriter writer = null;
            try
            {
                writer = new StreamWriter(logFilePath, true, Encoding.UTF8);
                writer.WriteLine("------------------------------------------------------------------------------");
                writer.WriteLine("出错时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                writer.WriteLine("错误信息:" + filterContext.Exception.Message);
                writer.WriteLine("Controller:" + filterContext.Controller);
                writer.WriteLine("错误源:" + filterContext.Exception.Source);
                writer.WriteLine("堆栈信息:" + filterContext.Exception.StackTrace);
                writer.WriteLine("------------------------------------------------------------------------------");
            }
            catch
            {
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }  
    }

你可能感兴趣的:(net)