Skip to content

Commit

Permalink
Pass the GlobalTracerAccessor into OpenTracingLogger so that the Noop…
Browse files Browse the repository at this point in the history
…Tracer is not cached when the tracer hasn't been configured yet and logger providers are resolved. (#50)
  • Loading branch information
yelob authored and austinlparker committed Sep 5, 2019
1 parent 7f292f6 commit 00df9de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
17 changes: 11 additions & 6 deletions src/OpenTracing.Contrib.NetCore/Logging/OpenTracingLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using OpenTracing.Contrib.NetCore.Internal;
using OpenTracing.Noop;
using OpenTracing.Util;

namespace OpenTracing.Contrib.NetCore.Logging
{
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;
}

Expand All @@ -26,8 +28,10 @@ public IDisposable BeginScope<TState>(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<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
Expand All @@ -38,7 +42,8 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
return;
}

ISpan span = _tracer.ActiveSpan;
ITracer tracer = _globalTracerAccessor.GetGlobalTracer();
ISpan span = tracer.ActiveSpan;

if (span == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/// <inheritdoc/>
public ILogger CreateLogger(string categoryName)
{
return new OpenTracingLogger(_tracer, categoryName);
return new OpenTracingLogger(_globalTracerAccessor, categoryName);
}

public void Dispose()
Expand Down

0 comments on commit 00df9de

Please sign in to comment.