创建错误日志到文件

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
    }

 

转载于:https://my.oschina.net/kongdf/blog/884867

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值