C# 记录日志
2021-07-01 19:05
标签:pre 文件 opened one sts str void files spl C# 记录日志 标签:pre 文件 opened one sts str void files spl 原文地址:https://www.cnblogs.com/-hao/p/9944683.html public static void WriteLog(string strLog)
{
string sFilePath = "d:\\FileLog\\" + DateTime.Now.ToString("yyyyMM");
string sFileName = "Log" + DateTime.Now.ToString("dd") + ".log";
sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
if (!Directory.Exists(sFilePath))//验证路径是否存在
{
Directory.CreateDirectory(sFilePath);
//不存在则创建
}
FileStream fs;
StreamWriter sw;
if (File.Exists(sFileName))
//验证文件是否存在,有则追加,无则创建
{
fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
}
else
{
fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
}
sw = new StreamWriter(fs);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " --- " + strLog);
sw.Close();
fs.Close();
}