Skip to content

Commit

Permalink
Check logging enabled before preparing OTel log message (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon authored Apr 24, 2024
1 parent b4a5664 commit 82cb184
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Elastic.OpenTelemetry/Diagnostics/LoggingEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class LoggingEventListener : EventListener, IAsyncDisposable
[GeneratedRegex(TraceParentRegularExpressionString)]
private static partial Regex TraceParentRegex();
#else

private static readonly Regex _traceParentRegex = new Regex(TraceParentRegularExpressionString);
private static Regex TraceParentRegex() => _traceParentRegex;
#endif
Expand Down Expand Up @@ -77,6 +76,11 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
return;
}

var logLevel = GetLogLevel(eventData);

if (!_logger.IsEnabled(logLevel))
return;

// This should generally be reasonably efficient but we can consider switching
// to a rented array and Span<char> if required.
var builder = StringBuilderCache.Acquire();
Expand All @@ -94,7 +98,7 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
// TODO - We can only get the OS thread ID from the args - Do we send that instead??
// As per this issue - https://github.com/dotnet/runtime/issues/13125 - OnEventWritten may be on a different thread
// so we can't use the Environment.CurrentManagedThreadId value here.
_logger.WriteLogLine(null, -1, timestamp, GetLogLevel(eventData), StringBuilderCache.GetStringAndRelease(builder), spanId);
_logger.WriteLogLine(null, -1, timestamp, logLevel, StringBuilderCache.GetStringAndRelease(builder), spanId);

static LogLevel GetLogLevel(EventWrittenEventArgs eventData) =>
eventData.Level switch
Expand Down

0 comments on commit 82cb184

Please sign in to comment.