public void CanResolveGenericFormatter() { // expected output TestLoggerFactory expected = BuildOptionsExpectedResult(); // actual output var services = new ServiceCollection(); services.AddOptions(); services.AddSingleton <TestLoggerFactory>(); services.AddSingleton <ILoggerFactory>(sp => sp.GetRequiredService <TestLoggerFactory>()); services.AddSingleton(typeof(ILogger <>), typeof(TestLogger <>)); services.AddSingleton(typeof(IOptionFormatter <>), typeof(DefaultOptionsFormatter <>)); services.AddSingleton <OptionsLogger, TestOptionsLogger>(); services.Configure <TestOptions>(options => options.IntField = 1); services.ConfigureFormatter <TestOptions>(); var servicesProvider = services.BuildServiceProvider(); servicesProvider.GetRequiredService <OptionsLogger>().LogOptions(); var logFormatters = servicesProvider.GetServices <IOptionFormatter>(); Assert.Single(logFormatters); Assert.True(logFormatters.First() is IOptionFormatter <TestOptions>); // ensure logging output is as expected var actual = servicesProvider.GetRequiredService <TestLoggerFactory>(); Assert.Equal(expected.ToString(), actual.ToString()); }
public void CustomFormatterResolverOverridesDefaultFormatter_PostRegistration() { // expected output TestLoggerFactory expected = BuildNamedOptionsExpectedResult(); // actual output var services = new ServiceCollection(); services.AddOptions(); services.AddSingleton <TestLoggerFactory>(); services.AddSingleton <ILoggerFactory>(sp => sp.GetRequiredService <TestLoggerFactory>()); services.AddSingleton(typeof(ILogger <>), typeof(TestLogger <>)); services.AddSingleton <OptionsLogger, TestOptionsLogger>(); // defaults services.TryConfigureFormatterResolver <TestOptions, TestOptionsFormatter2.Resolver>(); // configure options Enumerable .Range(1, 3) .ToList() .ForEach(i => { string name = i.ToString(); services.Configure <TestOptions>(name, (options => options.IntField = i)); services.ConfigureNamedOptionForLogging <TestOptions>(name); }); // post register overrides services.ConfigureFormatterResolver <TestOptions, TestOptionsFormatter.Resolver>(); var servicesProvider = services.BuildServiceProvider(); servicesProvider.GetRequiredService <OptionsLogger>().LogOptions(); var logFormatters = servicesProvider.GetServices <IOptionFormatter>(); Assert.Equal(3, logFormatters.Count()); Assert.True(logFormatters.First() is TestOptionsFormatter); Assert.True(logFormatters.ElementAt(1) is TestOptionsFormatter); Assert.True(logFormatters.ElementAt(2) is TestOptionsFormatter); var logFormatter = servicesProvider.GetService <IOptionFormatterResolver <TestOptions> >(); Assert.True(logFormatter is TestOptionsFormatter.Resolver); var actual = servicesProvider.GetRequiredService <TestLoggerFactory>(); Assert.Equal(expected.ToString(), actual.ToString()); }
public void FormatterConfiguredTwiceDoesNotLeadToDuplicatedFormatter() { // expected output TestLoggerFactory expected = BuildOptionsExpectedResult(); // actual output var services = new ServiceCollection(); services.AddOptions(); services.AddSingleton <TestLoggerFactory>(); services.AddSingleton <ILoggerFactory>(sp => sp.GetRequiredService <TestLoggerFactory>()); services.AddSingleton(typeof(ILogger <>), typeof(TestLogger <>)); services.AddSingleton <OptionsLogger, TestOptionsLogger>(); services.Configure <TestOptions>(options => options.IntField = 1); services.ConfigureFormatter <TestOptions, TestOptionsFormatter2>(); //the formatter configured second time will override the first one in DI and //DI will end up with two formatter for the same option services.ConfigureFormatter <TestOptions, TestOptionsFormatter>(); var servicesProvider = services.BuildServiceProvider(); servicesProvider.GetRequiredService <OptionsLogger>().LogOptions(); // only one options formater and it points to the right one var logFormatters = servicesProvider.GetServices <IOptionFormatter>(); Assert.Single(logFormatters); Assert.True(logFormatters.First() is TestOptionsFormatter); Assert.True(logFormatters.First() is IOptionFormatter <TestOptions>); // when resolving singe type specific formatter, we get the right one var logFormatter = servicesProvider.GetService <IOptionFormatter <TestOptions> >(); Assert.True(logFormatter is TestOptionsFormatter); // ensure logging output is as expected var actual = servicesProvider.GetRequiredService <TestLoggerFactory>(); Assert.Equal(expected.ToString(), actual.ToString()); }
public void NamedGenericFormatterGoldenPath() { // expected output TestLoggerFactory expected = BuildNamedOptionsExpectedResult(); // actual output var services = new ServiceCollection(); services.AddOptions(); services.AddSingleton <TestLoggerFactory>(); services.AddSingleton <ILoggerFactory>(sp => sp.GetRequiredService <TestLoggerFactory>()); services.AddSingleton(typeof(ILogger <>), typeof(TestLogger <>)); services.AddSingleton(typeof(IOptionFormatter <>), typeof(DefaultOptionsFormatter <>)); services.AddSingleton(typeof(IOptionFormatterResolver <>), typeof(DefaultOptionsFormatterResolver <>)); services.AddSingleton <OptionsLogger, TestOptionsLogger>(); Enumerable .Range(1, 3) .ToList() .ForEach(i => { string name = i.ToString(); services.Configure <TestOptions>(name, (options => options.IntField = i)); services.ConfigureNamedOptionForLogging <TestOptions>(name); }); var servicesProvider = services.BuildServiceProvider(); servicesProvider.GetRequiredService <OptionsLogger>().LogOptions(); var logFormatters = servicesProvider.GetServices <IOptionFormatter>(); Assert.Equal(3, logFormatters.Count()); var logFormatter = servicesProvider.GetService <IOptionFormatterResolver <TestOptions> >(); var actual = servicesProvider.GetRequiredService <TestLoggerFactory>(); Assert.Equal(expected.ToString(), actual.ToString()); }
public void CustomFormatterOverridesDefaultFormatter_PostRegistration() { // expected output TestLoggerFactory expected = BuildOptionsExpectedResult(); // actual output var services = new ServiceCollection(); services.AddOptions(); services.AddSingleton <TestLoggerFactory>(); services.AddSingleton <ILoggerFactory>(sp => sp.GetRequiredService <TestLoggerFactory>()); services.AddSingleton(typeof(ILogger <>), typeof(TestLogger <>)); services.AddSingleton <OptionsLogger, TestOptionsLogger>(); services.Configure <TestOptions>(options => options.IntField = 1); // default services.TryConfigureFormatter <TestOptions, TestOptionsFormatter2>(); // post register overrides services.ConfigureFormatter <TestOptions, TestOptionsFormatter>(); var servicesProvider = services.BuildServiceProvider(); servicesProvider.GetRequiredService <OptionsLogger>().LogOptions(); var logFormatters = servicesProvider.GetServices <IOptionFormatter>(); Assert.Single(logFormatters); Assert.True(logFormatters.First() is TestOptionsFormatter); Assert.True(logFormatters.First() is IOptionFormatter <TestOptions>); // when resolving singe type specific formatter, we get the right one var logFormatter = servicesProvider.GetService <IOptionFormatter <TestOptions> >(); Assert.True(logFormatter is TestOptionsFormatter); // ensure logging output is as expected var actual = servicesProvider.GetRequiredService <TestLoggerFactory>(); Assert.Equal(expected.ToString(), actual.ToString()); }