Rainmaker
Rainmaker

Reputation: 632

If I use Opentracing , do I need to use NLog again?

I log application tracing informations using Jaeger.

Do I need to use other log package again?

Upvotes: 3

Views: 583

Answers (2)

Rolf Kristensen
Rolf Kristensen

Reputation: 19867

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);
        }
    }

Upvotes: 0

Gordon
Gordon

Reputation: 316999

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.

Upvotes: 2

Related Questions