防止日志打印失败造成的程序闪退

This commit is contained in:
nilaoda 2023-09-19 22:56:56 +08:00
parent 836a90800b
commit e0ba6acd60
1 changed files with 28 additions and 21 deletions

View File

@ -70,32 +70,39 @@ namespace N_m3u8DL_RE.Common.Log
private static void HandleLog(string write, string subWrite = "") private static void HandleLog(string write, string subWrite = "")
{ {
if (subWrite == "") try
{ {
AnsiConsole.MarkupLine(write); if (subWrite == "")
}
else
{
AnsiConsole.Markup(write);
Console.WriteLine(subWrite);
}
if (IsWriteFile && File.Exists(LogFilePath))
{
var plain = write.RemoveMarkup() + subWrite.RemoveMarkup();
try
{ {
//进入写入 AnsiConsole.MarkupLine(write);
LogWriteLock.EnterWriteLock(); }
using (StreamWriter sw = File.AppendText(LogFilePath)) else
{
AnsiConsole.Markup(write);
Console.WriteLine(subWrite);
}
if (IsWriteFile && File.Exists(LogFilePath))
{
var plain = write.RemoveMarkup() + subWrite.RemoveMarkup();
try
{ {
sw.WriteLine(plain); //进入写入
LogWriteLock.EnterWriteLock();
using (StreamWriter sw = File.AppendText(LogFilePath))
{
sw.WriteLine(plain);
}
}
finally
{
//释放占用
LogWriteLock.ExitWriteLock();
} }
} }
finally }
{ catch (Exception)
//释放占用 {
LogWriteLock.ExitWriteLock(); Console.WriteLine("Failed to write: " + write);
}
} }
} }