/// <summary>
 /// </summary>
 /// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
 /// <param name="settings">The settings to apply to created <see cref="ConsoleLogger"/>'s.</param>
 /// <returns></returns>
 public static ILoggerFactory AddConsole(
     this ILoggerFactory factory,
     IConsoleLoggerSettings settings)
 {
     factory.AddProvider(new ConsoleLoggerProvider(settings));
     return(factory);
 }
 /// <summary>
 /// Adds a console logger that is enabled for <see cref="LogLevel"/>.Information or higher.
 /// </summary>
 /// <param name="factory"></param>
 /// <param name="settings"></param>
 public static ILoggerFactory AddConsoleAdvanced(
     this ILoggerFactory factory,
     IConsoleLoggerSettings settings)
 {
     factory.AddConsoleAdvanced((n, l) => l >= LogLevel.Information, settings);
     return(factory);
 }
 public IConsoleLoggerSettings Reload()
 {
     _settings    = _settings.Reload();
     _changeToken = new ConfigurationReloadToken();
     _settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
     return(this);
 }
예제 #4
0
        private void OnConfigurationReload(object state)
        {
            try
            {
                // The settings object needs to change here, because the old one is probably holding on
                // to an old change token.
                _settings = _settings.Reload();

                _includeScopes = _settings?.IncludeScopes ?? false;

                var scopeProvider = GetScopeProvider();
                foreach (var logger in _loggers.Values)
                {
                    logger.Filter        = GetFilter(logger.Name, _settings);
                    logger.ScopeProvider = scopeProvider;
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine($"Error while loading configuration changes.{Environment.NewLine}{ex}");
            }
            finally
            {
                // The token will change each time it reloads, so we need to register again.
                if (_settings?.ChangeToken != null)
                {
                    _settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DynamicLoggerProvider"/> class.
 /// </summary>
 /// <param name="filter">Default log level filter</param>
 /// <param name="includeScopes">Enable log scoping</param>
 public DynamicLoggerProvider(Func <string, LogLevel, bool> filter, bool includeScopes)
 {
     _delegate      = new ConsoleLoggerProvider(filter, includeScopes);
     _filter        = filter ?? _falseFilter;
     _settings      = null;
     _includeScopes = includeScopes;
 }
 public CloudFoundryLoggerSettings(IConfiguration configuration)
 {
     _changeToken = new ConfigurationReloadToken();
     _settings    = new ConfigurationConsoleLoggerSettings(configuration);
     _settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
     DynamicSwitches = new Dictionary <string, LogLevel>();
 }
예제 #7
0
 public void Dispose()
 {
     _delegate?.Dispose();
     _delegate = null;
     _settings = null;
     _loggers  = null;
 }
 /// <summary>
 /// Adds a console logger that is enabled for <see cref="LogLevel"/>s of minLevel or higher.
 /// </summary>
 /// <param name="factory"></param>
 /// <param name="minLevel">The minimum <see cref="LogLevel"/> to be logged</param>
 /// <param name="settings"></param>
 public static ILoggerFactory AddConsoleAdvanced(
     this ILoggerFactory factory,
     LogLevel minLevel,
     IConsoleLoggerSettings settings)
 {
     factory.AddConsoleAdvanced((category, logLevel) => logLevel >= minLevel, settings);
     return(factory);
 }
        /// <summary>
        /// Adds a console logger that is enabled as defined by the filter function.
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="filter"></param>
        /// <param name="settings"></param>
        public static ILoggerFactory AddConsoleAdvanced(
            this ILoggerFactory factory,
            Func <string, LogLevel, bool> filter,
            IConsoleLoggerSettings settings)
        {
            factory.AddProvider(new AdvancedConsoleLoggerProvider(filter, settings));

            return(factory);
        }
예제 #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DynamicLoggerProvider"/> class.
 /// </summary>
 /// <param name="settings">Logging Settings</param>
 public DynamicLoggerProvider(IConsoleLoggerSettings settings)
 {
     _delegate = new ConsoleLoggerProvider(settings);
     _settings = settings;
     SetFiltersFromSettings();
     if (_settings.ChangeToken != null)
     {
         _settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
     }
 }
예제 #11
0
 public ConsoleLoggerTests()
 {
     _settings = Substitute.For <IConsoleLoggerSettings>();
     _settings.LogLevel.Returns(LogLevel.Info);
     _settings.InfoForeColor.Returns(ConsoleColor.Blue);
     _settings.WarningForeColor.Returns(ConsoleColor.Yellow);
     _settings.ErrorForeColor.Returns(ConsoleColor.Red);
     _settings.MessageFormat.Returns("{0}\t{1:yyyy-MM-dd_HH:mm}:\t{2}");
     _console = Substitute.For <IConsole>();
     _logger  = new Logger.ConsoleLogger(_settings, _console);
 }
예제 #12
0
        public ConsoleLoggerProvider(Func<string, LogLevel, bool> filter, bool includeScopes)
        {
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }

            _filter = filter;
            _settings = new ConsoleLoggerSettings()
            {
                IncludeScopes = includeScopes,
            };
        }
예제 #13
0
        public ColorizedConsoleLoggerProvider(Func <string, LogLevel, bool> filter, bool includeScopes)
        {
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }

            _filter   = filter;
            _settings = new ConsoleLoggerSettings()
            {
                IncludeScopes = includeScopes,
            };
        }
예제 #14
0
 /// <summary>Adds a console logger named 'ConsoleAdvanced' to the factory.</summary>
 /// <param name="builder">The <see cref="T:Microsoft.Extensions.Logging.ILoggingBuilder" /> to use.</param>
 /// <param name="settings">The formatting settings, timezone, etc.</param>
 public static ILoggingBuilder AddConsoleAdvanced(this ILoggingBuilder builder,
                                                  IConsoleLoggerSettings settings)
 {
     if (settings == null)
     {
         throw new ArgumentNullException(nameof(settings));
     }
     builder.Services.TryAddEnumerable(
         ServiceDescriptor.Singleton <ILoggerProvider, AdvancedConsoleLoggerProvider>());
     builder.Services.TryAddEnumerable(
         ServiceDescriptor.Singleton(provider => settings));
     return(builder);
 }
예제 #15
0
        public ConsoleLoggerProvider(IConsoleLoggerSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            _settings = settings;

            if (_settings.ChangeToken != null)
            {
                _settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
            }
        }
예제 #16
0
        public ConsoleLoggerProvider(IConsoleLoggerSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            _settings = settings;

            if (_settings.ChangeToken != null)
            {
                _settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
            }
        }
예제 #17
0
 private Func <string, LogLevel, bool> GetFilter(string name, IConsoleLoggerSettings settings)
 {
     if (settings != null)
     {
         foreach (var keyPrefix in GetKeyPrefixes(name))
         {
             LogLevel level;
             if (settings.TryGetSwitch(keyPrefix, out level))
             {
                 return((n, l) => l >= level);
             }
         }
     }
     return((n, l) => false);
 }
예제 #18
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // Cleanup
                    _delegate?.Dispose();
                    _delegate = null;
                    _settings = null;
                    _loggers  = null;
                }

                disposed = true;
            }
        }
예제 #19
0
        private void OnConfigurationReload(object state)
        {
            // The settings object needs to change here, because the old one is probably holding on
            // to an old change token.
            _settings = _settings.Reload();

            foreach (var logger in _loggers.Values)
            {
                logger.Filter        = GetFilter(logger.Name, _settings);
                logger.IncludeScopes = _settings.IncludeScopes;
            }

            // The token will change each time it reloads, so we need to register again.
            if (_settings?.ChangeToken != null)
            {
                _settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
            }
        }
예제 #20
0
        private void OnConfigurationReload(object state)
        {
            // The settings object needs to change here, because the old one is probably holding on
            // to an old change token.
            _settings = _settings.Reload();

            foreach (var logger in _loggers.Values)
            {
                logger.Filter = GetFilter(logger.Name, _settings);
                logger.IncludeScopes = _settings.IncludeScopes;
            }

            // The token will change each time it reloads, so we need to register again.
            if (_settings?.ChangeToken != null)
            {
                _settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
            }
        }
        public AdvancedConsoleLoggerProvider(Func <string, LogLevel, bool> filter, IConsoleLoggerSettings settings)
        {
            //if (filter == null)
            //{
            //    throw new ArgumentNullException(nameof(filter));
            //}

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            _filter   = filter;
            _settings = settings;

            if (_settings.ChangeToken != null)
            {
                _settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
            }
        }
        private Func<string, LogLevel, bool> GetFilter(string name, IConsoleLoggerSettings settings)
        {
            if (_filter != null)
            {
                return _filter;
            }

            if (settings != null)
            {
                foreach (var prefix in GetKeyPrefixes(name))
                {
                    LogLevel level;
                    if (settings.TryGetSwitch(prefix, out level))
                    {
                        return (n, l) => l >= level;
                    }
                }
            }

            return (n, l) => false;
        }
 public EntityFrameworkLoggerProvider(string nameOrConnectionString, IConsoleLoggerSettings settings)
 {
     _nameOrConnectionString = nameOrConnectionString;
     _settings = settings;
 }
예제 #24
0
 public ConsoleLogger(IConsoleLoggerSettings loggerSettings, IConsole console) : base(loggerSettings)
 {
     _loggerSettings = loggerSettings;
     _console        = console ?? throw new ArgumentNullException(nameof(console));
 }
 public AdvancedConsoleLoggerProvider(IConsoleLoggerSettings settings)
     : this(null, settings)
 {
 }
 public static ILoggerFactory AddConsole(
     this ILoggerFactory factory,
     IConsoleLoggerSettings settings)
 {
     factory.AddProvider(new ConsoleLoggerProvider(settings));
     return factory;
 }
예제 #27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DynamicLoggerProvider"/> class.
 /// </summary>
 /// <param name="settings">Logging Settings</param>
 public DynamicLoggerProvider(IConsoleLoggerSettings settings)
 {
     _delegate = new ConsoleLoggerProvider(settings);
     _settings = settings;
     SetFiltersFromSettings();
 }
        public EntityFrameworkLoggerProvider(string nameOrConnectionString, Func<string, LogLevel, bool> filter)
        {
            _filter = filter;
            _nameOrConnectionString = nameOrConnectionString;

            _settings = new ConsoleLoggerSettings
            {
                IncludeScopes = false,
            };
        }