【问题标题】:If I use Opentracing , do I need to use NLog again?如果我使用 Opentracing ,我需要再次使用 NLog 吗?
【发布时间】:2019-01-08 02:20:09
【问题描述】:

我使用 Jaeger 记录应用程序跟踪信息。

是否需要重新使用其他日志包?

【问题讨论】:

    标签: logging opentracing jaeger


    【解决方案1】:

    OpenTracing 是分布式跟踪的框架。因此,它更多的是关于性能监控和可观察性而不是日志记录(NLog 是关于什么的)。

    OpenTracing 允许您手动检测代码以生成具有相关跨度的跟踪,其中包含有关您的应用中代码执行的信息。这包括使用错误和任意键和值来注释跨度,您可以使用它们来代替日志记录。但是,这与专用结构化日志记录不同。

    【讨论】:

    • 很好的答案。我只想补充一件事:跟踪通常是“采样的”,这意味着并非所有请求都被记录,而日志记录没有被采样,这意味着所有事件都被记录。在某些情况下,您希望记录所有内容,但不跟踪所有内容。
    • @jpkrohling 我们不在 Instana 上采样,但我想它适合其他供应商 :)
    【解决方案2】:

    可以使用此目标将 LogEvents 从 NLog 转发到 OpenTracing:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 2022-08-04
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多