C#写日志文件

/// <summary>
/// 写日志文件
/// </summary>
/// <param name="sMsg"></param>
public static void WriteLog(string sMsg)
{
if(sMsg != "")
{
Random randObj = new Random( DateTime.Now.Millisecond );
int file = randObj.Next()+1;
string filename = DateTime.Now.ToString("yyyyMMddhhmmss") + file + ".txt";
try
{
  FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath("log\\" + filename));
  if (!fi.Exists)
  {
   using (StreamWriter sw = fi.CreateText())
   {
   sw.WriteLine(DateTime.Now + "\n" +sMsg + "\n");
   sw.Close();
   }
}
   else
  {
   using (StreamWriter sw = fi.AppendText())
   {
   sw.WriteLine(DateTime.Now + "\n" +sMsg + "\n");
   sw.Close();
   }
   }
}
catch
{
}
}
}
posted @ 2006-12-30 15:10  '.Elvis.'  阅读(1419)  评论(0编辑  收藏  举报