zoukankan      html  css  js  c++  java
  • log日志

      public class CustomHandleLogAttribute : 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("--------------------------------------------------------------------------------------");
                    }
                }
                finally
                {
                    if (writer != null)
                        writer.Close();
                }
            }
        }
  • 相关阅读:
    ThinkPHP3.2 分组分模块
    PHP 视频
    微信分享SDK
    【mysql】一维数据TopN的趋势图
    【日期-时间】Java中Calendar的使用
    【java消息格式化】使用MessageFormat进行消息格式化
    【Java数据格式化】使用DecimalFormat 对Float和double进行格式化
    【xargs使用】查询包含某字符串的所有文件
    【SVN】自动备份SVN仓库
    【Oozie】安装配置Oozie
  • 原文地址:https://www.cnblogs.com/tgdjw/p/5053527.html
Copyright © 2011-2022 走看看