Skip to content

Commit

Permalink
😊 调整 文件日志写入代码,添加 try/catch 容错处理
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkSoul committed Apr 21, 2024
1 parent 3f9447e commit 999a6c3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,24 @@ internal async Task WriteAsync(LogMessage logMsg, bool flush)
{
if (_textWriter == null) return;

CheckForNewLogFile();
await _textWriter.WriteLineAsync(logMsg.Message);
try
{
CheckForNewLogFile();
await _textWriter.WriteLineAsync(logMsg.Message);

if (flush)
if (flush)
{
await _textWriter.FlushAsync();
}
}
catch (Exception ex)
{
await _textWriter.FlushAsync();
// 处理文件写入错误
if (_options.HandleWriteError != null)
{
var fileWriteError = new FileWriteError(_fileName, ex);
_options.HandleWriteError(fileWriteError);
}
}
}

Expand Down
20 changes: 16 additions & 4 deletions framework/Furion/Logging/Implantations/File/FileLoggingWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,24 @@ internal async Task WriteAsync(LogMessage logMsg, bool flush)
{
if (_textWriter == null) return;

CheckForNewLogFile();
await _textWriter.WriteLineAsync(logMsg.Message);
try
{
CheckForNewLogFile();
await _textWriter.WriteLineAsync(logMsg.Message);

if (flush)
if (flush)
{
await _textWriter.FlushAsync();
}
}
catch (Exception ex)
{
await _textWriter.FlushAsync();
// 处理文件写入错误
if (_options.HandleWriteError != null)
{
var fileWriteError = new FileWriteError(_fileName, ex);
_options.HandleWriteError(fileWriteError);
}
}
}

Expand Down

0 comments on commit 999a6c3

Please sign in to comment.