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文件

  • 相关阅读:
    StatusBar
    iOS开发UI篇-UITabBarController简单介绍
    iOS最全梳理
    UITabbarController的UITabbarItem(例:"我的")点击时,判断是否登录
    iOS APP 发布上架流程
    IOS开发
    IT教育课程考评系统开发-26
    IT教育课程考评系统开发-25
    2020100201-1
    IT教育课程考评系统开发-24
  • 原文地址:https://www.cnblogs.com/Dtscal/p/2727353.html
Copyright © 2011-2022 走看看