3

I log application tracing informations using Jaeger.

Do I need to use other log package again?

Rainmaker
  • 583
  • 3
  • 13

2 Answers2

2

OpenTracing is a framework for Distributed Tracing. As such, it is more about performance monitoring and observability than logging (what NLog is about).

OpenTracing allows you to manually instrument your code to generate traces with relevant spans containing information about code execution in your app. This includes annotating spans with errors and arbitrary keys and values, which you could use instead of logging. However, that's not the same as dedicated structured logging.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 1
    Great answer. I would just complement with one more thing: tracing is usually "sampled", meaning that not all requests are recorded, whereas logging is not sampled, meaning that all events are recorded. There are cases where you want to log everything, but not trace everything. – jpkroehling Jan 23 '19 at 10:56
  • 1
    @jpkrohling we don't sample at Instana, but I guess it's right for other vendors :) – Gordon Jan 23 '19 at 14:02
0

One can forward LogEvents from NLog to OpenTracing using this target:

    [Target("OpenTracing")]
    public class OpenTracingTarget : TargetWithContext
    {
        private readonly OpenTracing.ITracer _tracer;

        public bool SetTagErrorOnException { get; set; }

        public OpenTracingTarget()
            :this(null)
        {
        }

        public OpenTracingTarget(OpenTracing.ITracer tracer)
        {
            _tracer = tracer ?? OpenTracing.Util.GlobalTracer.Instance;
            ContextProperties.Add(new TargetPropertyWithContext("component", "${logger}"));
            ContextProperties.Add(new TargetPropertyWithContext("level", "${level}"));
            ContextProperties.Add(new TargetPropertyWithContext("message", "${message}"));
            ContextProperties.Add(new TargetPropertyWithContext("event", "${event-properties:EventId_Name}") { IncludeEmptyValue = false });
            ContextProperties.Add(new TargetPropertyWithContext("eventid", "${event-properties:EventId_Id}") { IncludeEmptyValue = false });
        }

        protected override void Write(LogEventInfo logEvent)
        {
            var span = _tracer.ActiveSpan;
            if (span == null)
                return;

            if (SetTagErrorOnException && logEvent.Exception != null)
            {
                span.SetTag(OpenTracing.Tag.Tags.Error, true);
            }

            var fields = GetAllProperties(logEvent);
            if (logEvent.Exception != null)
            {
                fields[OpenTracing.LogFields.ErrorKind] = logEvent.Exception.GetType().ToString();
                fields[OpenTracing.LogFields.ErrorObject] = logEvent.Exception;
            }

            span.Log(fields);
        }
    }
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70