From 00df9debca5b2979acdaaeacf885030b3546ca28 Mon Sep 17 00:00:00 2001 From: yelob Date: Thu, 5 Sep 2019 10:38:26 -0700 Subject: [PATCH] Pass the GlobalTracerAccessor into OpenTracingLogger so that the NoopTracer is not cached when the tracer hasn't been configured yet and logger providers are resolved. (#50) --- .../Logging/OpenTracingLogger.cs | 17 +++++++++++------ .../Logging/OpenTracingLoggerProvider.cs | 13 +++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/OpenTracing.Contrib.NetCore/Logging/OpenTracingLogger.cs b/src/OpenTracing.Contrib.NetCore/Logging/OpenTracingLogger.cs index e7ab975..49a4994 100644 --- a/src/OpenTracing.Contrib.NetCore/Logging/OpenTracingLogger.cs +++ b/src/OpenTracing.Contrib.NetCore/Logging/OpenTracingLogger.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using Microsoft.Extensions.Logging; using OpenTracing.Contrib.NetCore.Internal; +using OpenTracing.Noop; +using OpenTracing.Util; namespace OpenTracing.Contrib.NetCore.Logging { @@ -9,12 +11,12 @@ internal class OpenTracingLogger : ILogger { private const string OriginalFormatPropertyName = "{OriginalFormat}"; - private readonly ITracer _tracer; private readonly string _categoryName; + private readonly IGlobalTracerAccessor _globalTracerAccessor; - public OpenTracingLogger(ITracer tracer, string categoryName) + public OpenTracingLogger(IGlobalTracerAccessor globalTracerAccessor, string categoryName) { - _tracer = tracer; + _globalTracerAccessor = globalTracerAccessor; _categoryName = categoryName; } @@ -26,8 +28,10 @@ public IDisposable BeginScope(TState state) public bool IsEnabled(LogLevel logLevel) { // Filtering should be done via the general Logging filtering feature. - - return !_tracer.IsNoopTracer(); + ITracer tracer = _globalTracerAccessor.GetGlobalTracer(); + return !( + (tracer is NoopTracer) || + (tracer is GlobalTracer && !GlobalTracer.IsRegistered())); } public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) @@ -38,7 +42,8 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except return; } - ISpan span = _tracer.ActiveSpan; + ITracer tracer = _globalTracerAccessor.GetGlobalTracer(); + ISpan span = tracer.ActiveSpan; if (span == null) { diff --git a/src/OpenTracing.Contrib.NetCore/Logging/OpenTracingLoggerProvider.cs b/src/OpenTracing.Contrib.NetCore/Logging/OpenTracingLoggerProvider.cs index d657d9d..a516654 100644 --- a/src/OpenTracing.Contrib.NetCore/Logging/OpenTracingLoggerProvider.cs +++ b/src/OpenTracing.Contrib.NetCore/Logging/OpenTracingLoggerProvider.cs @@ -10,24 +10,17 @@ namespace OpenTracing.Contrib.NetCore.Logging [ProviderAlias("OpenTracing")] internal class OpenTracingLoggerProvider : ILoggerProvider { - private readonly ITracer _tracer; + private readonly IGlobalTracerAccessor _globalTracerAccessor; public OpenTracingLoggerProvider(IGlobalTracerAccessor globalTracerAccessor) { - // HACK: We can't use ITracer directly here because this would lead to a StackOverflowException - // (due to a circular dependency) if the ITracer needs a ILoggerFactory. - // https://github.com/opentracing-contrib/csharp-netcore/issues/14 - - if (globalTracerAccessor == null) - throw new ArgumentNullException(nameof(globalTracerAccessor)); - - _tracer = globalTracerAccessor.GetGlobalTracer(); + _globalTracerAccessor = globalTracerAccessor ?? throw new ArgumentNullException(nameof(globalTracerAccessor)); } /// public ILogger CreateLogger(string categoryName) { - return new OpenTracingLogger(_tracer, categoryName); + return new OpenTracingLogger(_globalTracerAccessor, categoryName); } public void Dispose()