日志记录

如有不明白的地方欢迎加QQ群 14670545 探讨
/// <summary>
        /// Application_Error:当应用程序中遇到一个未处理的异常时,该事件被触发。 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();

            if (ex != null && ex.TargetSite.Name != "CheckVirtualFileExists")
            {
                string log = Request.Url.AbsoluteUri + "\t" + Request.HttpMethod + "\t" + ex.Message + "\r\n" + ex.StackTrace + "\r\n" + "\r\n--------------------------------------------------\r\n";
                byte[] buffer = Encoding.Default.GetBytes(log);

                string dir = Path.Combine(AppPath.PhysicalPath, "logfiles\\server\\");
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                FileStream fs = new FileStream(dir + String.Format("{0}.log", DateTime.Now.ToString("yyyyMMdd")), FileMode.Create | FileMode.Append);
                try
                {
                    fs.Write(buffer, 0, buffer.Length);
                    Function.ClientAlert(ex.Message);
                }
                finally
                {
                    fs.Close();
                }
            }
            Server.ClearError();
        }

你可能感兴趣的:(日志记录)