-
-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathIServiceCollectionExtensions.cs
More file actions
56 lines (46 loc) · 1.8 KB
/
IServiceCollectionExtensions.cs
File metadata and controls
56 lines (46 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace Microsoft.Extensions.DependencyInjection;
internal static class IServiceCollectionExtensions
{
public static IServiceCollection AddConfiguration(this IServiceCollection services, string? cultureName = null)
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile("appsettings.json");
if (cultureName != null)
{
builder.AddInMemoryCollection(new Dictionary<string, string?>()
{
["BootstrapBlazorOptions:DefaultCultureInfo"] = cultureName,
["Logging:LogLevel:Default"] = "Information"
});
}
var config = builder.Build();
services.AddSingleton<IConfiguration>(config);
return services;
}
public static ILoggingBuilder AddMockLoggerProvider(this ILoggingBuilder builder)
{
return builder.AddProvider(new TestLoggerProvider());
}
class TestLoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName) => new TestLogger();
public void Dispose() { }
}
class TestLogger : ILogger
{
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
return null;
}
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
}
}
}