Exemplo n.º 1
0
 public void Start()
 {
     if (_allListenersSubscription == null)
     {
         if (_tracer.IsNoopTracer() && !_options.StartInstrumentationForNoopTracer)
         {
             _logger.LogWarning("Instrumentation has not been started because no tracer was registered.");
         }
         else
         {
             _logger.LogTrace("Starting AllListeners subscription");
             _allListenersSubscription = DiagnosticListener.AllListeners.Subscribe(this);
         }
     }
 }
Exemplo n.º 2
0
        public void Emit(LogEvent logEvent)
        {
            ISpan span = _tracer.ActiveSpan;

            if (span == null)
            {
                // Creating a new span for a log message seems brutal so we ignore messages if we can't attach it to an active span.
                return;
            }

            if (_tracer.IsNoopTracer())
            {
                return;
            }

            if (_filter(logEvent))
            {
                return;
            }

            var fields = new Dictionary <string, object>();

            try
            {
                fields[LogFields.Event]   = "log";
                fields[LogFields.Message] = logEvent.RenderMessage();
                fields["level"]           = logEvent.Level;

                if (logEvent.Exception != null)
                {
                    fields[LogFields.ErrorKind]   = logEvent.Exception.GetType().FullName;
                    fields[LogFields.ErrorObject] = logEvent.Exception;
                }

                foreach (var property in logEvent.Properties)
                {
                    fields[property.Key] = property.Value.ToString();
                }
            }
            catch (Exception logException)
            {
                fields["opentracing.contrib.netcore.error"] = logException.ToString();
            }

            span.Log(fields);
        }