// IMPORTANT: For all logging frameworks, do not set any default values for
        //            "dd.trace_id" and "dd.span_id" when initializing the subscriber
        //            because the Tracer may be initialized at a time when it is not safe
        //            to add properties logging context of the underlying logging framework.
        //
        //            Failure to abide by this can cause a SerializationException when
        //            control is passed from one AppDomain to another where the originating
        //            AppDomain used a logging framework that stored logging context properties
        //            inside the System.Runtime.Remoting.Messaging.CallContext structure
        //            but the target AppDomain is unable to de-serialize the object --
        //            this can easily happen if the target AppDomain cannot find/load the
        //            logging framework assemblies.
        public LibLogScopeEventSubscriber(Tracer tracer, IScopeManager scopeManager, string defaultServiceName, string version, string env)
        {
            _tracer             = tracer;
            _scopeManager       = scopeManager;
            _defaultServiceName = defaultServiceName;
            _version            = version;
            _env = env;

#if NETFRAMEWORK
            if (_executingIISPreStartInit)
            {
                _scopeManager.TraceStarted += OnTraceStarted_RefreshIISState;
            }
#endif

            try
            {
                _logProvider = LogProvider.CurrentLogProvider ?? LogProvider.ResolveLogProvider();
                if (_logProvider is SerilogLogProvider)
                {
                    // Do not set default values for Serilog because it is unsafe to set
                    // except at the application startup, but this would require auto-instrumentation
                    _scopeManager.SpanOpened += StackOnSpanOpened;
                    _scopeManager.SpanClosed += StackOnSpanClosed;

                    if (_logProvider is CustomSerilogLogProvider customSerilogLogProvider)
                    {
                        _serilogEnricher = customSerilogLogProvider.CreateEnricher(tracer);
                    }
                }
                else if (_logProvider is CustomNLogLogProvider)
                {
                    _nlogEnricher = new NLogEnricher(tracer);

                    _scopeManager.TraceStarted += RegisterNLogEnricher;
                    _scopeManager.TraceEnded   += ClearNLogEnricher;
                }
                else
                {
                    _scopeManager.SpanActivated += MapOnSpanActivated;
                    _scopeManager.TraceEnded    += MapOnTraceEnded;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Could not successfully start the LibLogScopeEventSubscriber. There was an issue resolving the application logger.");
            }
        }
 public Context(ILogProvider logProvider, NLogEnricher enricher)
 {
     try
     {
         _environment = logProvider.OpenMappedContext(CorrelationIdentifier.EnvKey, enricher._environmentProperty);
         _version     = logProvider.OpenMappedContext(CorrelationIdentifier.VersionKey, enricher._versionProperty);
         _service     = logProvider.OpenMappedContext(CorrelationIdentifier.ServiceKey, enricher._serviceProperty);
         _traceId     = logProvider.OpenMappedContext(CorrelationIdentifier.TraceIdKey, enricher._traceIdProperty);
         _spanId      = logProvider.OpenMappedContext(CorrelationIdentifier.SpanIdKey, enricher._spanIdProperty);
     }
     catch
     {
         // Clear the properties that are already mapped
         Dispose();
         throw;
     }
 }