Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化日志写入性能,公开后台写入日志相关的方法 #33

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions src/dotnetCampus.Logger/Writers/Helpers/ICoreLogWriter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;

#if NET6_0_OR_GREATER
using System.Threading.Channels;
#else
using System.Collections.Concurrent;
#endif

namespace dotnetCampus.Logging.Writers.Helpers;

/// <summary>
/// 提供各种不同线程安全方式的最终日志写入功能。
/// </summary>
internal interface ICoreLogWriter
public interface ICoreLogWriter
{
/// <summary>
/// 写入日志。
Expand Down Expand Up @@ -78,32 +83,89 @@ public void Do(Action action)
internal sealed class ProducerConsumerLogWriter : ICoreLogWriter
{
private readonly Action<string> _logger;
#if NET6_0_OR_GREATER
private readonly Channel<object> _queue = Channel.CreateUnbounded<object>(new UnboundedChannelOptions
{
SingleReader = true,
SingleWriter = false,
AllowSynchronousContinuations = false,
});
#else
private readonly BlockingCollection<object> _queue = new();
#endif

/// <summary>
/// 创建 <see cref="ProducerConsumerLogWriter"/> 的新实例,并启动消费线程。
/// </summary>
public ProducerConsumerLogWriter(Action<string> logger)
{
_logger = logger;
#if NET6_0_OR_GREATER
_ = Task.Run(Consume);
#else
new Task(Consume, TaskCreationOptions.LongRunning).Start();
#endif
}

/// <inheritdoc />
public void Write(string? message)
{
if (message is not null)
{
#if NET6_0_OR_GREATER
_queue.Writer.TryWrite(message);
#else
_queue.Add(message);
#endif
}
}

/// <inheritdoc />
public void Do(Action action)
{
#if NET6_0_OR_GREATER
_queue.Writer.TryWrite(action);
#else
_queue.Add(action);
#endif
}

#if NET6_0_OR_GREATER
/// <summary>
/// 消费队列中的元素。
/// </summary>
private async Task Consume()
{
while (true)
{
var success = await _queue.Reader.WaitToReadAsync();
if (!success)
{
break;
}

while (_queue.Reader.TryRead(out var item))
{
try
{
switch (item)
{
case string message:
_logger(message);
break;
case Action action:
action();
break;
}
}
catch (Exception)
{
// 本次日志发生了异常,已经无法继续写入日志,只能抛弃异常。
}
}
}
}
#else
/// <summary>
/// 消费队列中的元素。
/// </summary>
Expand All @@ -122,4 +184,5 @@ private void Consume()
}
}
}
#endif
}
2 changes: 1 addition & 1 deletion src/dotnetCampus.Logger/Writers/LogWritingThreadMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public enum LogWritingThreadMode
/// <summary>
/// 包含 <see cref="LogWritingThreadMode"/> 的扩展方法。
/// </summary>
internal static class LogWritingThreadModeExtensions
public static class LogWritingThreadModeExtensions
{
/// <summary>
/// 根据 <see cref="LogWritingThreadMode"/> 创建对应的 <see cref="ICoreLogWriter"/> 实例。
Expand Down