public void OnNext(object diagnosticListenerInstance)
        {
            if (_diagnosticListenerObserver == null)
            {
                return;
            }

            DiagnosticListenerStub diagnosticListenerStub;

            try
            {
                diagnosticListenerStub = DiagnosticListenerStub.Wrap(diagnosticListenerInstance);
            }
            catch (Exception ex)
            {
                Log.Error(ErrorUtil.DynamicInvokerLogComponentMoniker,
                          $" {nameof(_diagnosticListenerObserver)}.{nameof(OnNext)}(..) cannot be invoked:"
                          + $" Could not create a {nameof(DiagnosticListenerStub)} for the {nameof(diagnosticListenerInstance)}"
                          + $" instance passed into {nameof(OnNext)}(..). Does it have the wrong runtime type?"
                          + $" Such a type mismatch should never happen becasue we use IObserver<object> where an"
                          + $" IObserver<DiagnosticListener> was expected, so the passed instance should always be of type"
                          + $" 'DiagnosticListener'. See earlier logged error for detailed type info.",
                          ex);
                return;
            }

            try
            {
                _diagnosticListenerObserver.OnNext(diagnosticListenerStub);
            }
            catch (Exception ex)
            {
                throw LogAndRethrowEscapedError(ex, nameof(OnNext));
            }
        }
        internal static bool TryWrap(object diagnosticListenerInstance, out DiagnosticListenerStub diagnosticListenerStub)
        {
            if (diagnosticListenerInstance == null)
            {
                diagnosticListenerStub = NoOpSingeltons.DiagnosticListenerStub;
                return(true);
            }

            DynamicInvoker_DiagnosticListener invoker = DynamicInvoker.Current.DiagnosticListener;

            if (invoker != null && invoker.TryGetInvokerHandleForInstance(diagnosticListenerInstance, out DynamicInvokerHandle <DynamicInvoker_DiagnosticListener> handle))
            {
                diagnosticListenerStub = new DiagnosticListenerStub(diagnosticListenerInstance, handle);
                return(true);
            }
            else
            {
                diagnosticListenerStub = NoOpSingeltons.DiagnosticListenerStub;
                return(false);
            }
        }
 public bool TryAsDiagnosticListener(out DiagnosticListenerStub diagnosticListener)
 {
     return(DiagnosticListenerStub.TryWrap(_diagnosticSourceInstance, out diagnosticListener));
 }