我的appsettings.json文件中有一个值,我想在appnsight.config文件中使用它。
这很容易实现吗?还是会过于复杂?我对c#不太熟悉(我对powershell没问题(
以下是我到目前为止的设置:appsettings.json
{
"AppKey": {
"AppKey": "2"
}
}
appinsight.config
<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<InstrumentationKey>appsettingskey</InstrumentationKey>
主要部分:
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config/hosting.json", optional: true)
.AddCommandLine(args)
.Build();
我还是个新手,还在学习,所以请耐心等待
如果您希望遥测记录到应用程序洞察,只需将值存储在Microsoft.ApplicationInsights.AspNetCore包的appsettings.json中即可获得值
{
"ApplicationInsights": {
"InstrumentationKey": "11111111-2222-3333-4444-555555555555"
}
}
然后将json添加到生成器
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
这是一个很棒的漫游
您可以通过配置映射来获取配置文件的值。以下是我在NETCORE环境中的使用方式,您可以参考,如果您使用的是netframework,可能会有不同的配置。
1.配置环境
安装:Microsoft.Extensions.Options.ConfigurationExtensions
2.配置appsetting.json在appsetting.JSON.中配置映射类的JSON节点
{
"AppKeys": {
"AppKey": "2"
}
}
3.新的映射类,将您的配置结构映射到类属性
public class AppKeys
{
public string AppKey{ get; set; }
}
4.添加配置映射
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AppKeys>(Configuration.GetSection("AppKeys"));
}
5.使用访问值,例如:
var key = Configuration["AppKeys:AppKey"]
6.使用DI注入获得配置
public class name
{
private readonly AppKeys classname;
public RedisClient(IOptions<AppKeys> value)
{
classname = value.Value;
}
}