/// <summary>
/// 写日志的方法,每天一个文件夹,每小时一个文本
/// </summary>
/// <param name="msg">内容</param>
public static void LogWrite(string msg)
{
try
{
string filePath_ml = AppDomain.CurrentDomain.BaseDirectory + "Log";
if (!Directory.Exists(filePath_ml))
{
Directory.CreateDirectory(filePath_ml);
}
string filePath_ml1 = filePath_ml+"/"+DateTime.Now.ToString("yyyyMMdd");
if (!Directory.Exists(filePath_ml1))
{
Directory.CreateDirectory(filePath_ml1);
}
string filename = DateTime.Now.ToString("yyyyMMddhh") + ".txt";
string filePath = filePath_ml1 +"/"+ filename;
FileInfo file = new FileInfo(filePath);
StringBuilder sb = new StringBuilder();
sb.Append(DateTime.Now.ToString());
sb.Append(" ");
sb.Append(msg);
FileMode fm = new FileMode();
if (!file.Exists)
{
fm = FileMode.Create;
}
else
{
fm = FileMode.Append;
}
using (FileStream fs = new FileStream(filePath, fm, FileAccess.Write, FileShare.Write))
{
using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
{
//sw.WriteLine("******************************************");
sw.WriteLine(sb.ToString());
sw.Close();
}
}
}
catch (Exception ex)
{
//return;
}
}
每天自动生成文件夹,每小时生成log文件
于 2023-03-02 14:17:14 首次发布