首页 > 代码库 > 简单日志

简单日志

/// <summary>
/// Common 的摘要描述
/// </summary>
public class Common: System.Web.UI.Page
{
public Common()
{
//
// TODO: 在此加入建構函式的程式碼
//
}

public void Log(string userid, Exception lastError)
{
string path = Server.MapPath("~/LOG/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/LOG"));
if (!dir.Exists)
{
dir.Create();
}
FileInfo file = new FileInfo(path);

FileStream fs = null;
if (!file.Exists)
{
fs = file.Create();
fs.Dispose();
}
file = null;
StreamWriter sw = new StreamWriter(path, true);
sw.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "--" + userid + ":" + lastError.Message + "\r\n" + lastError.StackTrace);
sw.Close();
sw.Dispose();
}
}