public void ReloadHappyPath()
        {
            var configSource = new TestConfigurationSource();

            configSource.Provider.Set("CustomLogger:Level", "Warn");

            var configuration = new ConfigurationBuilder()
                                .Add(configSource)
                                .Build();

            var services = new ServiceCollection();

            services.Configure <LoggerOptions>("MyLogger", configuration.GetSection("CustomLogger"));

            var serviceProvider = services.BuildServiceProvider();

            var logProcessor     = new Mock <ILogProcessor>().Object;
            var name             = "MyLogger";
            var logProviders     = new[] { new Mock <ILogProvider>().Object };
            var contextProviders = new[] { new Mock <IContextProvider>().Object };
            var optionsMonitor   = serviceProvider.GetRequiredService <IOptionsMonitor <LoggerOptions> >();
            var options          = optionsMonitor.Get("MyLogger");
            Action <LoggerOptions> configureOptions = null;

            var reloadingLogger = ReloadingLogger.New(logProcessor, name, logProviders, contextProviders, optionsMonitor, options, configureOptions);

            var errorHandler = new Mock <IErrorHandler>().Object;

            reloadingLogger.ErrorHandler = errorHandler;

            ILogger logger1 = reloadingLogger._logger;

            logger1.Level.Should().Be(LogLevel.Warn);
            logger1.IsDisabled.Should().BeFalse();
            logger1.ErrorHandler.Should().BeSameAs(errorHandler);

            configSource.Provider.Set("CustomLogger:Level", "Debug");
            configSource.Provider.Set("CustomLogger:IsDisabled", "true");
            configSource.Provider.Reload();

            ILogger logger2 = reloadingLogger._logger;

            logger2.Should().NotBeSameAs(logger1);
            logger2.Level.Should().Be(LogLevel.Debug);
            logger2.IsDisabled.Should().BeTrue();
            logger2.ErrorHandler.Should().BeSameAs(errorHandler);
        }
        public void ConstructorHappyPath()
        {
            var source = new TestConfigurationSource();

            source.Provider.Set("CustomLogger:Level", "Fatal");

            var configuration = new ConfigurationBuilder()
                                .Add(source)
                                .Build();

            var services = new ServiceCollection();

            services.Configure <LoggerOptions>("MyLogger", configuration.GetSection("CustomLogger"));

            var serviceProvider = services.BuildServiceProvider();

            var logProcessor     = new Mock <ILogProcessor>().Object;
            var name             = "MyLogger";
            var logProviders     = new[] { new Mock <ILogProvider>().Object };
            var contextProviders = new[] { new Mock <IContextProvider>().Object };
            var optionsMonitor   = serviceProvider.GetRequiredService <IOptionsMonitor <LoggerOptions> >();
            var options          = optionsMonitor.Get("MyLogger");
            Action <LoggerOptions> configureOptions = opt => { };

            var reloadingLogger = ReloadingLogger.New(logProcessor, name, logProviders, contextProviders, optionsMonitor, options, configureOptions);

            string        actualName         = reloadingLogger.Name;
            ILogProcessor actualLogProcessor = reloadingLogger._logProcessor;
            IReadOnlyCollection <ILogProvider>     actualLogProviders     = reloadingLogger._logProviders;
            IReadOnlyCollection <IContextProvider> actualContextProviders = reloadingLogger._contextProviders;
            Action <LoggerOptions> actualConfigureOptions = reloadingLogger._configureOptions;

            actualName.Should().BeSameAs(name);
            actualLogProcessor.Should().BeSameAs(logProcessor);
            actualLogProviders.Should().BeSameAs(logProviders);
            actualContextProviders.Should().BeSameAs(contextProviders);
            actualConfigureOptions.Should().BeSameAs(configureOptions);

            Logger logger = reloadingLogger._logger;

            logger.Name.Should().Be(name);
            logger.Level.Should().Be(LogLevel.Fatal);
            logger.IsDisabled.Should().BeFalse();
            logger.LogProviders.Should().BeSameAs(logProviders);
            logger.ContextProviders.Should().BeSameAs(contextProviders);
            logger.LogProcessor.Should().BeSameAs(logProcessor);
        }
Beispiel #3
0
        public void ReloadHappyPath()
        {
            var source = new TestConfigurationSource();

            source.Provider.Set("CustomLogProvider:Foo", "123");
            source.Provider.Set("CustomLogProvider:Bar", "abc");

            var configuration = new ConfigurationBuilder()
                                .Add(source)
                                .Build();

            var services = new ServiceCollection();

            services.Configure <TestOptions>("MyLogger", configuration.GetSection("CustomLogProvider"));

            var serviceProvider = services.BuildServiceProvider();

            var optionsMonitor = serviceProvider.GetRequiredService <IOptionsMonitor <TestOptions> >();
            var options        = optionsMonitor.Get("MyLogger");
            Func <TestOptions, ILogProvider> createLogProvider = o =>
                                                                 new TestLogProvider {
                Foo = o.Foo, Bar = o.Bar
            };
            string name = "MyLogger";
            Action <TestOptions> configureOptions = null;

            var reloadingLogProvider = ReloadingLogProviderOfTestOptions.New(optionsMonitor, options, createLogProvider, name, configureOptions);

            TestLogProvider logProvider1 = reloadingLogProvider._logProvider;

            logProvider1.Foo.Should().Be(123);
            logProvider1.Bar.Should().Be("abc");

            source.Provider.Set("CustomLogProvider:Foo", "456");
            source.Provider.Set("CustomLogProvider:Bar", "xyz");
            source.Provider.Reload();

            TestLogProvider logProvider2 = reloadingLogProvider._logProvider;

            logProvider2.Should().NotBeSameAs(logProvider1);
            logProvider2.Foo.Should().Be(456);
            logProvider2.Bar.Should().Be("xyz");
        }
Beispiel #4
0
        public void ConstructorHappyPath()
        {
            var source = new TestConfigurationSource();

            source.Provider.Set("CustomLogProvider:Foo", "123");
            source.Provider.Set("CustomLogProvider:Bar", "abc");

            var configuration = new ConfigurationBuilder()
                                .Add(source)
                                .Build();

            var services = new ServiceCollection();

            services.Configure <TestOptions>("MyLogger", configuration.GetSection("CustomLogProvider"));

            var serviceProvider = services.BuildServiceProvider();

            var optionsMonitor = serviceProvider.GetRequiredService <IOptionsMonitor <TestOptions> >();
            var options        = optionsMonitor.Get("MyLogger");
            Func <TestOptions, ILogProvider> createLogProvider = o =>
                                                                 new TestLogProvider {
                Foo = o.Foo, Bar = o.Bar
            };
            string name = "MyLogger";
            Action <TestOptions> configureOptions = opt => { };

            var reloadingLogProvider = ReloadingLogProviderOfTestOptions.New(optionsMonitor, options, createLogProvider, name, configureOptions);

            string actualName = reloadingLogProvider._name;

            actualName.Should().BeSameAs(name);

            TestLogProvider logProvider = reloadingLogProvider._logProvider;

            logProvider.Foo.Should().Be(123);
            logProvider.Bar.Should().Be("abc");

            Action <TestOptions> actualConfigureOptions = reloadingLogProvider._configureOptions;

            actualConfigureOptions.Should().BeSameAs(configureOptions);
        }