Skip to content

Commit

Permalink
防止日志打印失败造成的程序闪退
Browse files Browse the repository at this point in the history
  • Loading branch information
nilaoda committed Sep 19, 2023
1 parent 836a908 commit e0ba6ac
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/N_m3u8DL-RE.Common/Log/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,39 @@ private static string GetCurrTime()

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

Expand Down

0 comments on commit e0ba6ac

Please sign in to comment.