2013-0502

MVC自定义HandleErrorAttribute实现记录日志功能

作者: momy 分类: 编程开发 0 Comment »
摘要:通过自定义HandleErrorAttribute实现记录日志功能。

首先FilterConfig添加

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new CustomHandleErrorAttribute());
}


自定义HandleErrorAttribute如下:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
    public class CustomHandleErrorAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            SaveExceptionAndError(filterContext);
            base.OnException(filterContext);
        }
        private void SaveExceptionAndError(ExceptionContext exceptionContext)
        {
            string errortime = string.Empty;
            string erroraddr = string.Empty;
            string errorinfo = string.Empty;
            string errorsource = string.Empty;
            string errortrace = string.Empty;
            errortime = "发生时间: " + System.DateTime.Now.ToString();
            erroraddr = "异常位置: " + exceptionContext.RequestContext.HttpContext.Request.Url.ToString();
            errorinfo = "异常信息: " + exceptionContext.Exception.Message;
            errorsource = "错误源:" + exceptionContext.Exception.Source;
            errortrace = "堆栈信息:" + exceptionContext.Exception.StackTrace;
            //独占方式,因为文件只能由一个进程写入.
            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() + ".log";
                    path = exceptionContext.RequestContext.HttpContext.Server.MapPath("~/ErrorLogs/") + year + "/" + month;
                    //如果目录不存在则创建
                    if (!System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }
                    System.IO.FileInfo file = new System.IO.FileInfo(path + "/" + filename);
    
                    writer = new System.IO.StreamWriter(file.FullName, true);//文件不存在就创建,true表示追加
                    writer.WriteLine("用户IP:" + exceptionContext.RequestContext.HttpContext.Request.UserHostAddress);
                    writer.WriteLine(errortime);
                    writer.WriteLine(erroraddr);
                    writer.WriteLine(errorinfo);
                    writer.WriteLine(errorsource);
                    writer.WriteLine(errortrace);
                    writer.WriteLine("--------------------------------------------------------------------------------------");
                    //writer.Close();
                }
            }
            finally
            {
                if (writer != null)
                    writer.Close();
            }
        }
    }


标签: HandleErrorAttribute MVC4 CustomHandleErrorAttribute 阅读: 20307
上一篇: HTML5安全风险详析之一:CORS攻击 - 10572次
下一篇: Windows Server 2012十大快捷键功能解析 - 11899次

向右滑动解锁留言