ASP.NET中记录Log到txt文件的方法:
Code
public static void addLogErr(string SPName, string ErrDescribe)
{
string tracingFile = "C:/inetpub/wwwroot/log/"; //Server.MapPath("/log/")
if (!System.IO.Directory.Exists(tracingFile))
System.IO.Directory.CreateDirectory(tracingFile);
string fileName = DateTime.Now.ToString("yyyyMMdd") + ".txt";
tracingFile += fileName;
if (tracingFile != String.Empty)
{
System.IO.FileInfo file = new System.IO.FileInfo(tracingFile);
System.IO.StreamWriter debugWriter = new System.IO.StreamWriter(file.Open(System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite));
debugWriter.WriteLine(SPName + " (" + System.DateTime.Now.ToString() + ") " + " :");
debugWriter.WriteLine(ErrDescribe);
debugWriter.WriteLine();
debugWriter.Flush();
debugWriter.Close();
}
}
调用方法:
Code
addLogErr("ApplyForm", "originalReason:"+originalReason);
Log示例:
Code
ApplyForm (2008-11-13 20:13:48) :
originalReason:5LiA5ZOB6buR5p2/5pyJ6ZmQ5YWs5Y+45LiA5ZOB6buR5p2/5pyJ6Zm