使用 AddInMemoryCollection 設定 Configuration 進行單元測試

設定 appsettings.json 如下

1
2
3
4
5
6
7
8
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

轉換為 Dictionary<string, string>

1
2
3
4
Dictionary<string, string> inMemorySettings = new Dictionary<string, string> {
{"Logging:LogLevel:Default", "Information"},
{"Logging:LogLevel:Microsoft.AspNetCore", "Warning"},
};

建立 Configuration

1
2
3
IConfiguration configuration = new ConfigurationBuilder()
.AddInMemoryCollection(inMemorySettings)
.Build();