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
{
}
}
}
/// 写日志文件
/// </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
{
}
}
}