|
2 | 2 | using Microsoft.Extensions.DependencyInjection; |
3 | 3 | using Microsoft.Extensions.DependencyInjection.Extensions; |
4 | 4 | using Microsoft.Extensions.Logging.Configuration; |
5 | | -using System.Diagnostics.CodeAnalysis; |
6 | 5 | using Xunit.Abstractions; |
7 | 6 |
|
8 | 7 | namespace Microsoft.Extensions.Logging; |
9 | 8 |
|
10 | 9 | public static class MELXunitLoggerExtensions |
11 | 10 | { |
12 | | - private static bool TryGetService<T>(this IServiceProvider services, [NotNullWhen(returnValue: true)] out T? service) where T : class |
13 | | - { |
14 | | - service = services.GetService<T>(); |
15 | | - return service is not null; |
16 | | - } |
17 | | - |
18 | 11 | #region "ILoggingBuilder" |
19 | | - public static ILoggingBuilder AddXunit(this ILoggingBuilder builder) |
| 12 | + public static ILoggingBuilder AddXunit(this ILoggingBuilder builder, Action<XunitLoggerOptions>? configure = null) |
20 | 13 | { |
21 | 14 | LoggerProviderOptions.RegisterProviderOptions<XunitLoggerOptions, XunitLoggerProvider>(builder.Services); |
| 15 | + if (configure is not null) { builder.Services.Configure(configure); } |
22 | 16 | builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, XunitLoggerProvider>()); |
23 | 17 | return builder; |
24 | 18 | } |
25 | 19 |
|
26 | | - public static ILoggingBuilder AddXunit(this ILoggingBuilder builder, Action<XunitLoggerOptions>? configure) |
27 | | - { |
28 | | - var optionsBuilder = builder.Services.AddOptions<XunitLoggerOptions>(); |
29 | | - if (configure is not null) { optionsBuilder.Configure(configure); } |
30 | | - builder.Services.TryAddEnumerable( |
31 | | - ServiceDescriptor.Singleton<ILoggerProvider>(sp => |
32 | | - { |
33 | | - if (sp.TryGetService<ITestOutputHelper>(out var testOutput)) { ActivatorUtilities.CreateInstance<XunitLoggerProvider>(sp,testOutput); } |
34 | | - if (sp.TryGetService<IMessageSink>(out var messageSink)) { ActivatorUtilities.CreateInstance<XunitLoggerProvider>(sp, messageSink); } |
35 | | - return Microsoft.Extensions.Logging.Abstractions.NullLoggerProvider.Instance; |
36 | | - })); |
37 | | - |
38 | | - return builder; |
39 | | - } |
40 | | - |
41 | 20 | public static ILoggingBuilder AddXunit(this ILoggingBuilder builder, ITestOutputHelper output, Action<XunitLoggerOptions>? configure = null) |
42 | 21 | { |
43 | | - var optionsBuilder = builder.Services.AddOptions<XunitLoggerOptions>(); |
44 | | - if (configure is not null) { optionsBuilder.Configure(configure); } |
45 | | - builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider>(sp=> ActivatorUtilities.CreateInstance<XunitLoggerProvider>(sp, output))); |
46 | | - return builder; |
| 22 | + builder.Services.AddSingleton(output); |
| 23 | + return builder.AddXunit(configure); |
47 | 24 | } |
48 | 25 |
|
49 | 26 | public static ILoggingBuilder AddXunit(this ILoggingBuilder builder, IMessageSink output, Action<XunitLoggerOptions>? configure = null) |
50 | 27 | { |
51 | | - var optionsBuilder = builder.Services.AddOptions<XunitLoggerOptions>(); |
52 | | - if (configure is not null) { optionsBuilder.Configure(configure); } |
53 | | - builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider>(sp => ActivatorUtilities.CreateInstance<XunitLoggerProvider>(sp, output))); |
54 | | - return builder; |
| 28 | + builder.Services.AddSingleton(output); |
| 29 | + return builder.AddXunit(configure); |
55 | 30 | } |
56 | 31 | #endregion |
57 | 32 |
|
|
0 commit comments