Skip to content

Commit

Permalink
使控制台日志记录器支持直接从构造函数创建。
Browse files Browse the repository at this point in the history
  • Loading branch information
walterlv committed Aug 22, 2024
1 parent 019c6fb commit dbfc583
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/dotnetCampus.Logger/Writers/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace dotnetCampus.Logging.Writers;

/// <summary>
/// 在控制台输出日志的日志记录器。
/// </summary>
public class ConsoleLogger : ILogger
{
/// <summary>
Expand All @@ -17,6 +20,16 @@ public class ConsoleLogger : ILogger

private readonly RepeatLoggerDetector _repeat;

/// <summary>
/// 创建一个 <see cref="ConsoleLogger"/> 的新实例。
/// </summary>
/// <param name="threadMode">指定控制台日志的线程安全模式。</param>
/// <param name="mainArgs">Main 方法的参数。</param>
public ConsoleLogger(LogWritingThreadMode threadMode = LogWritingThreadMode.NotThreadSafe, string[]? mainArgs = null)
: this(threadMode.CreateCoreLogWriter(), TagFilterManager.FromCommandLineArgs(mainArgs ?? []))
{
}

internal ConsoleLogger(ICoreLogWriter coreWriter, TagFilterManager? tagManager)
{
_repeat = new RepeatLoggerDetector(ClearAndMoveToLastLine);
Expand Down
25 changes: 24 additions & 1 deletion src/dotnetCampus.Logger/Writers/LogWritingThreadMode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace dotnetCampus.Logging.Writers;
using System;
using dotnetCampus.Logging.Writers.Helpers;

namespace dotnetCampus.Logging.Writers;

/// <summary>
/// 表示如何管理日志的写入线程。
Expand All @@ -23,3 +26,23 @@ public enum LogWritingThreadMode
/// </remarks>
ProducerConsumer,
}

/// <summary>
/// 包含 <see cref="LogWritingThreadMode"/> 的扩展方法。
/// </summary>
internal static class LogWritingThreadModeExtensions
{
/// <summary>
/// 根据 <see cref="LogWritingThreadMode"/> 创建对应的 <see cref="ICoreLogWriter"/> 实例。
/// </summary>
/// <param name="threadMode">线程安全模式。</param>
/// <returns>最终日志写入器。</returns>
/// <exception cref="ArgumentOutOfRangeException">当线程安全模式不支持时抛出。</exception>
public static ICoreLogWriter CreateCoreLogWriter(this LogWritingThreadMode threadMode) => threadMode switch
{
LogWritingThreadMode.NotThreadSafe => new NotThreadSafeLogWriter(),
LogWritingThreadMode.Lock => new LockLogWriter(),
LogWritingThreadMode.ProducerConsumer => new ProducerConsumerLogWriter(),
_ => throw new ArgumentOutOfRangeException(nameof(threadMode)),
};
}

0 comments on commit dbfc583

Please sign in to comment.