/// <summary>
        /// Constructor.
        /// </summary>
        public ExceptionLoggingScopesSubscriber(LoggingScopes loggingScopes)
        {
            _subscription = (_, args) =>
            {
                var exceptionScopes = args.Exception.TryFindLoggingScopes() ?? ImmutableStack <ILoggingScope> .Empty;
                var currentScopes   = loggingScopes.CurrentScopes ?? ImmutableStack <ILoggingScope> .Empty;

                // Combine the exception scopes and current scopes:
                // - Start with the exception scopes.
                // - Add in any current scopes that are not already included in the exception scopes.

                foreach (var scope in currentScopes.Reverse().Where(x => !exceptionScopes.Contains(x)))
                {
                    exceptionScopes = exceptionScopes.Push(scope);
                }

                if (!exceptionScopes.IsEmpty)
                {
                    args.Exception.SetLoggingScopes(exceptionScopes);
                }
            };
            AppDomain.CurrentDomain.FirstChanceException += _subscription;
        }
Esempio n. 2
0
 /// <summary>
 /// Creates the logging provider.
 /// </summary>
 public LoggingScopeTrackingLoggerProvider(LoggingScopes loggingScopes)
 {
     _loggingScopes = loggingScopes;
     _logger        = new Logger(this);
 }