本文整理汇总了C#中Microsoft.Framework.ConfigurationModel.Configuration.GetSubKey方法的典型用法代码示例。如果您正苦于以下问题:C# Configuration.GetSubKey方法的具体用法?C# Configuration.GetSubKey怎么用?C# Configuration.GetSubKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Framework.ConfigurationModel.Configuration
的用法示例。
在下文中一共展示了Configuration.GetSubKey方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Configure
public void Configure(IApplicationBuilder app)
{
var config = new Configuration(
new MemoryConfigurationSource
{
{"MySettings:RetryCount", "42"},
{"MySettings:DefaultAdBlock", "House"},
{"MySettings:AdBlock:House:ProductCode", "123"},
{"MySettings:AdBlock:House:Origin", "blob-456"},
{"MySettings:AdBlock:Contoso:ProductCode", "contoso2014"},
{"MySettings:AdBlock:Contoso:Origin", "sql-789"},
});
var mySettings = new MySettings();
mySettings.Read(config.GetSubKey("MySettings"));
app.Run(async ctx =>
{
ctx.Response.ContentType = "text/plain";
await ctx.Response.WriteAsync(string.Format("Retry Count {0}\r\n", mySettings.RetryCount));
await ctx.Response.WriteAsync(string.Format("Default Ad Block {0}\r\n", mySettings.DefaultAdBlock));
foreach (var adBlock in mySettings.AdBlocks.Values)
{
await ctx.Response.WriteAsync(string.Format(
"Ad Block {0} Origin {1} Product Code {2}\r\n",
adBlock.Name, adBlock.Origin, adBlock.ProductCode));
}
});
}
示例2: Startup
public Startup(
IHostingEnvironment hostingEnvironment,
ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<Startup>();
var configuration = new Configuration();
configuration.AddJsonFile("config.json");
configuration.AddEnvironmentVariables();
var loggingConfiguration = configuration.GetSubKey("Logging");
var serilog = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.WithMachineName()
.Enrich.WithProcessId()
.Enrich.WithThreadId();
if (string.Equals(hostingEnvironment.EnvironmentName, "Development", StringComparison.OrdinalIgnoreCase))
{
serilog.WriteTo.ColoredConsole();
}
string elasticSearchConnectionString;
if (loggingConfiguration.TryGet("ElasticSearch:Server", out elasticSearchConnectionString))
{
serilog.WriteTo.ElasticSearch(node: new Uri(elasticSearchConnectionString));
}
loggerFactory.AddSerilog(serilog);
}
示例3: CanGetSubKey
public void CanGetSubKey()
{
// Arrange
var dic1 = new Dictionary<string, string>()
{
{"Data:DB1:Connection1", "MemVal1"},
{"Data:DB1:Connection2", "MemVal2"}
};
var dic2 = new Dictionary<string, string>()
{
{"DataSource:DB2:Connection", "MemVal3"}
};
var dic3 = new Dictionary<string, string>()
{
{"Data", "MemVal4"}
};
var memConfigSrc1 = new MemoryConfigurationSource(dic1);
var memConfigSrc2 = new MemoryConfigurationSource(dic2);
var memConfigSrc3 = new MemoryConfigurationSource(dic3);
var config = new Configuration();
config.AddLoadedSource(memConfigSrc1);
config.AddLoadedSource(memConfigSrc2);
config.AddLoadedSource(memConfigSrc3);
string memVal1, memVal2, memVal3, memVal4, memVal5;
bool memRet1, memRet2, memRet3, memRet4, memRet5;
// Act
var configFocus = config.GetSubKey("Data");
memRet1 = configFocus.TryGet("DB1:Connection1", out memVal1);
memRet2 = configFocus.TryGet("DB1:Connection2", out memVal2);
memRet3 = configFocus.TryGet("DB2:Connection", out memVal3);
memRet4 = configFocus.TryGet("Source:DB2:Connection", out memVal4);
memRet5 = configFocus.TryGet(null, out memVal5);
// Assert
Assert.True(memRet1);
Assert.True(memRet2);
Assert.False(memRet3);
Assert.False(memRet4);
Assert.True(memRet5);
Assert.Equal("MemVal1", memVal1);
Assert.Equal("MemVal2", memVal2);
Assert.Equal("MemVal4", memVal5);
Assert.Equal("MemVal1", configFocus.Get("DB1:Connection1"));
Assert.Equal("MemVal2", configFocus.Get("DB1:Connection2"));
Assert.Null(configFocus.Get("DB2:Connection"));
Assert.Null(configFocus.Get("Source:DB2:Connection"));
Assert.Equal("MemVal4", configFocus.Get(null));
Assert.Equal("MemVal1", configFocus["DB1:Connection1"]);
Assert.Equal("MemVal2", configFocus["DB1:Connection2"]);
Assert.Null(configFocus["DB2:Connection"]);
Assert.Null(configFocus["Source:DB2:Connection"]);
Assert.Equal("MemVal4", configFocus[null]);
}