zoukankan      html  css  js  c++  java
  • asp.net 错误信息记录到日志文件

    昨天正式给上司汇报工作,大部分还比较满意.说道错误信息的处理方式怎么处理时有没有做错误日志;没,那就参考公司的的错误日志类操作类吧.

    在global.asax里面添加也是实现HttpApplication的application_error事件

    View Code
    void Application_Error(object sender, EventArgs e) 
        { 
            // Code that runs when an unhandled error occurs
            // 在出现未处理的错误时运行的代码
            string errorLog = Server.MapPath("~/App_Data/Error.log");
            System.IO.FileStream fs = new System.IO.FileStream(errorLog, System.IO.FileMode.Append, System.IO.FileAccess.Write);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
            Exception ex = Server.GetLastError();
            string IP = "";//客户端IP
            HttpRequest request = HttpContext.Current.Request;
            string result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            {
                IP = request.ServerVariables["REMOTE_ADDR"];
            }
            else if (request.ServerVariables["HTTP_VIA"] != null)
            {
                IP = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
            }
            else
            {
                IP = request.UserHostAddress;
            }
            if (ex.InnerException != null)
            {
                sw.WriteLine("IP:{0}\n时间:{1}\n错误消息:{2}\n位置:{3}\n地址:{4}\n\n", IP,DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss ffff"), ex.ToString(), ex.InnerException, Request.Url.AbsolutePath);
            }
            else if (ex != null)
            {
                sw.WriteLine("IP:{0}\n时间:{1}\n错误消息:{2}\n地址:{3}\n\n", IP,DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss ffff"), ex.ToString(), Request.Url.AbsolutePath);
            }
            else
            {
                sw.WriteLine("未知错误");
            }
            Server.ClearError();
            sw.Close(); 
        }

    当然还要在app_data目录下新建一个error_log.txt文件

  • 相关阅读:
    CPT104-labs
    Java-数据结构-ArrayList
    INT104-lab13[Parzen Window Method][此方法无数据集划分]
    INT104-lab12 [KNN Algorithm][lambda表达式]
    INT104-lab11 [聚类] [iris数据集] [K-means Algorithm]
    Nginx配置https兼容http
    JS获取整个网页html代码
    nginx重启生效conf文件的修改
    WampServer
    在win10系统中,开启hyper-v要满足下列条件
  • 原文地址:https://www.cnblogs.com/Dtscal/p/2727353.html
Copyright © 2011-2022 走看看