优化日志文件生成逻辑 (#412)
This commit is contained in:
parent
1905f0503f
commit
606db68dbb
|
@ -45,21 +45,20 @@ namespace N_m3u8DL_RE.Common.Log
|
|||
Directory.CreateDirectory(logDir);
|
||||
}
|
||||
|
||||
LogFilePath = Path.Combine(logDir, DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fff") + ".log");
|
||||
//若文件存在则加序号
|
||||
var now = DateTime.Now;
|
||||
LogFilePath = Path.Combine(logDir, now.ToString("yyyy-MM-dd_HH-mm-ss-fff") + ".log");
|
||||
int index = 1;
|
||||
var fileName = Path.GetFileNameWithoutExtension(LogFilePath);
|
||||
string init = "LOG " + now.ToString("yyyy/MM/dd") + Environment.NewLine
|
||||
+ "Save Path: " + Path.GetDirectoryName(LogFilePath) + Environment.NewLine
|
||||
+ "Task Start: " + now.ToString("yyyy/MM/dd HH:mm:ss") + Environment.NewLine
|
||||
+ "Task CommandLine: " + Environment.CommandLine;
|
||||
init += $"{Environment.NewLine}{Environment.NewLine}";
|
||||
//若文件存在则加序号
|
||||
while (File.Exists(LogFilePath))
|
||||
{
|
||||
LogFilePath = Path.Combine(Path.GetDirectoryName(LogFilePath)!, $"{fileName}-{index++}.log");
|
||||
}
|
||||
|
||||
string now = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
|
||||
string init = "LOG " + DateTime.Now.ToString("yyyy/MM/dd") + Environment.NewLine
|
||||
+ "Save Path: " + Path.GetDirectoryName(LogFilePath) + Environment.NewLine
|
||||
+ "Task Start: " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + Environment.NewLine
|
||||
+ "Task CommandLine: " + Environment.CommandLine;
|
||||
init += $"{Environment.NewLine}{Environment.NewLine}";
|
||||
File.WriteAllText(LogFilePath, init, Encoding.UTF8);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
Loading…
Reference in New Issue