Skip to content

Commit

Permalink
Merge pull request #180 from nmosafi/thread-id
Browse files Browse the repository at this point in the history
Capture caller's thread information into `LogInfo`
  • Loading branch information
neuecc committed Sep 12, 2024
2 parents c9abcea + a9d2410 commit d2fe96e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
10 changes: 9 additions & 1 deletion src/ZLogger/LogInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

namespace ZLogger;

public readonly struct LogInfo(LogCategory category, Timestamp timestamp, LogLevel logLevel, EventId eventId, Exception? exception, LogScopeState? scopeState, object? context = null, string? memberName = null, string? filePath = null, int lineNumber = 0)
public readonly struct LogInfo(LogCategory category, Timestamp timestamp, LogLevel logLevel, EventId eventId, Exception? exception, LogScopeState? scopeState, ThreadInfo? threadInfo, object? context = null, string? memberName = null, string? filePath = null, int lineNumber = 0)
{
public readonly LogCategory Category = category;
public readonly Timestamp Timestamp = timestamp;
public readonly LogLevel LogLevel = logLevel;
public readonly EventId EventId = eventId;
public readonly Exception? Exception = exception;
public readonly LogScopeState? ScopeState = scopeState;
public readonly ThreadInfo? ThreadInfo = threadInfo;
public readonly object? Context = context;
public readonly string? MemberName = memberName;
public readonly string? FilePath = filePath;
Expand All @@ -38,3 +39,10 @@ public override string ToString()
return Name;
}
}

public readonly struct ThreadInfo(int threadId, string? threadName, bool isThreadPoolThread)
{
public readonly int ThreadId = threadId;
public readonly string? ThreadName = threadName;
public readonly bool IsThreadPoolThread = isThreadPoolThread;
}
15 changes: 12 additions & 3 deletions src/ZLogger/ZLoggerLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public sealed class ZLoggerLogger : ILogger
readonly TimeProvider? timeProvider;
readonly IExternalScopeProvider? scopeProvider;
readonly bool formatImmediately;
readonly bool captureThreadInfo;

public ZLoggerLogger(string categoryName, IAsyncLogProcessor logProcessor, ZLoggerOptions options, IExternalScopeProvider? scopeProvider)
{
Expand All @@ -19,6 +20,7 @@ public ZLoggerLogger(string categoryName, IAsyncLogProcessor logProcessor, ZLogg
this.timeProvider = options.TimeProvider;
this.scopeProvider = scopeProvider;
this.formatImmediately = options.IsFormatLogImmediatelyInStandardLog;
this.captureThreadInfo = options.CaptureThreadInfo;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
Expand All @@ -31,12 +33,19 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
var callerFilePath = default(string?);
var callerLineNumber = default(int);
var context = default(object?);
if (state is IZLoggerAdditionalInfo)
if (state is IZLoggerAdditionalInfo additionalInfo)
{
(context, callerMemberName, callerFilePath, callerLineNumber) = ((IZLoggerAdditionalInfo)state).GetAdditionalInfo();
(context, callerMemberName, callerFilePath, callerLineNumber) = additionalInfo.GetAdditionalInfo();
}

var info = new LogInfo(category, new Timestamp(timeProvider), logLevel, eventId, exception, scopeState, context, callerMemberName, callerFilePath, callerLineNumber);
var threadInfo = default(ThreadInfo?);
if (captureThreadInfo)
{
var currentThread = Thread.CurrentThread;
threadInfo = new ThreadInfo(currentThread.ManagedThreadId, currentThread.Name, currentThread.IsThreadPoolThread);
}

var info = new LogInfo(category, new Timestamp(timeProvider), logLevel, eventId, exception, scopeState, threadInfo, context, callerMemberName, callerFilePath, callerLineNumber);

IZLoggerEntry entry;
if (state is VersionedLogState)
Expand Down
9 changes: 7 additions & 2 deletions src/ZLogger/ZLoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum BackgroundBufferFullMode
/// Wait until there's more room in the queue.
/// </summary>
Block,

/// <summary>
/// Drop the overflowing log entry.
/// </summary>
Expand Down Expand Up @@ -57,6 +57,11 @@ public class ZLoggerOptions
/// </summary>
public bool IsFormatLogImmediatelyInStandardLog { get; set; } = true;

/// <summary>
/// Capture information about the thread that generated a log entry. Default is false.
/// </summary>
public bool CaptureThreadInfo { get; set; } = false;

Func<IZLoggerFormatter> formatterFactory = DefaultFormatterFactory;

public ZLoggerOptions UseFormatter(Func<IZLoggerFormatter> formatterFactory)
Expand All @@ -80,4 +85,4 @@ static IZLoggerFormatter DefaultFormatterFactory()
{
return new PlainTextZLoggerFormatter();
}
}
}
16 changes: 13 additions & 3 deletions tests/ZLogger.Tests/CallerInfoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ public void CallerInfo()
var loggerFactory = LoggerFactory.Create(x =>
{
x.SetMinimumLevel(LogLevel.Debug);
x.AddZLoggerLogProcessor(_ => processor);
x.AddZLoggerLogProcessor(options =>
{
options.CaptureThreadInfo = true;
return processor;
});
});
var logger = loggerFactory.CreateLogger("test");

Expand All @@ -21,6 +25,12 @@ public void CallerInfo()
var x = processor.Entries.Dequeue();
x.LogInfo.MemberName.Should().Be("CallerInfo");
Path.GetFileName(x.LogInfo.FilePath).Should().Be("CallerInfoTest.cs");
x.LogInfo.LineNumber.Should().Be(19);
x.LogInfo.LineNumber.Should().Be(23);

x.LogInfo.ThreadInfo.Should().NotBeNull();
x.LogInfo.ThreadInfo!.Value.ThreadId.Should().Be(System.Environment.CurrentManagedThreadId);
x.LogInfo.ThreadInfo!.Value.IsThreadPoolThread.Should().Be(true);
x.LogInfo.ThreadInfo!.Value.ThreadName.Should().Be(".NET TP Worker");
}
}
}

0 comments on commit d2fe96e

Please sign in to comment.