[c#]代码库
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<string> ExceptonInfoQueue = new Queue<string>();
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);
}
}
}
}
});
}
}