zoukankan      html  css  js  c++  java
  • 创建错误日志到文件 kiddy

    View Code
     public class ErrorLog
        {
            /// <summary>
            /// 创建错误日志到文件
            /// </summary>
            /// <param name="exceptionMessage">异常说明</param>
            /// <param name="exceptionStackTrace">异常跟踪堆栈信息</param>
            public static void AddLog(string exceptionMessage, string exceptionStackTrace)
            {
                string requestType = HttpContext.Current.Request.HttpMethod;
                string strUrl = HttpContext.Current.Request.Url.AbsolutePath;
                string strIP = HttpContext.Current.Request.UserHostAddress;
                string filePath = ConfigurationManager.AppSettings["ErrorLogPath"].ToString();
                string serverPath = HttpContext.Current.Server.MapPath("~/" + filePath + "");
                //创建日志目录
                if (!Directory.Exists(serverPath))
                {
                    Directory.CreateDirectory(serverPath);
                }
                //指定日志文件目录
                string filename = serverPath + "\\"+Convert.ToDateTime(DateTime.Now).ToShortDateString()+".txt";
                if (File.Exists(filename))
                {
                    StreamWriter sw = File.AppendText(filename);
                    sw.Write("访问时间:" + DateTime.Now.ToString() + "       访问IP:" + strIP + "\r\n");
                    sw.Write("请求方式:" + requestType + "       请求地址:" + strUrl + "\r\n");
                    sw.Write("异常说明:" + exceptionMessage + "\r\n");
                    sw.Write("跟踪信息:" + exceptionStackTrace + "\r\n");
                    sw.Write("----------------------------------------------------------------------------\r\n");
                    sw.Flush();
                    sw.Close();
                }
                else
                {
                    StreamWriter sw = File.CreateText(filename);
                    sw.Write("访问时间:" + DateTime.Now.ToString() + "       访问IP:" + strIP + "\r\n");
                    sw.Write("请求方式:" + requestType + "       请求地址:" + strUrl + "\r\n");
                    sw.Write("异常说明:" + exceptionMessage + "\r\n");
                    sw.Write("跟踪信息:" + exceptionStackTrace + "\r\n");
                    sw.Write("----------------------------------------------------------------------------\r\n");
                    sw.Flush();
                    sw.Close();
                }
            }
    
            public static void ShowError(string exceptionMessage, string exceptionStackTrace)
            {
                AddLog(exceptionMessage, exceptionStackTrace);
    
                string errorHandlerPage = "~/errorpage.aspx?msg=" + HttpContext.Current.Server.UrlEncode(exceptionMessage);
                if (!string.IsNullOrEmpty(errorHandlerPage))
                {
                    HttpContext.Current.Response.Redirect(errorHandlerPage);
                }
            }
        }

     添加处理模块:

    View Code
     public class WebHttpModule : IHttpModule
        {
            #region IHttpModule 成员
    
            public void Dispose()
            {
                
            }
    
            public void Init(HttpApplication context)
            {
                //context.Error += new EventHandler(context_Error);
            }
    
            void context_Error(object sender, EventArgs e)
            {
                Exception ex;
                ex = HttpContext.Current.Server.GetLastError();
                string errrorMessage = ex == null ? string.Empty : ex.Message;
                string stackTrace = ex == null ? string.Empty : ex.StackTrace;
    
                //在这里将出错信息记录在文件日志中
                ErrorLog.ShowError(errrorMessage, stackTrace);
            }
    
            #endregion
        }
    道之所在,虽千万人吾往矣
  • 相关阅读:
    导航守卫
    Java内部类详解
    requestAnimationFrame,Web中写动画的另一种选择
    requestAnimationFrame
    Linux下文件的打包、解压缩指令——tar,gzip,bzip2
    selenium的动作链 actionchains
    列表存储内存的两种形式
    使用timeit模块 测试两种方式生成列表的所用的时间
    selenium访问百度 然后获取百度logo的截图
    python 666
  • 原文地址:https://www.cnblogs.com/kongdf/p/2566510.html
Copyright © 2011-2022 走看看