protected void Session_Start(Object sender, EventArgs e) { LogHelper.LogBasePath = Request.MapPath("/Log/"); } protected void Application_Error(Object sender, EventArgs e) { if (string.IsNullOrEmpty(LogHelper.LogBasePath)) { LogHelper.LogBasePath = Request.MapPath("/Log/"); } LogHelper.ExceptonInfoQueue.Enqueue(Server.GetLastError().ToString()); Response.Redirect("/Error.aspx"); } public class LogHelper { public static Queue ExceptonInfoQueue = new Queue(); public static string LogBasePath; static LogHelper() { ThreadPool.QueueUserWorkItem(o => { while (true) { if (ExceptonInfoQueue.Count > 0) { string str = ExceptonInfoQueue.Dequeue(); //写入错误信息 string strFileName = DateTime.Now.ToString(@"\yyyy-MM-dd") + ".txt"; string absoluteFileName = Path.Combine(LogBasePath, strFileName); lock (ExceptonInfoQueue) { using (FileStream fs = new FileStream(absoluteFileName, FileMode.Append, FileAccess.Write)) { byte[] data = Encoding.Default.GetBytes(str); fs.Write(data, 0, data.Length); } } } } }); } }