gpt4 book ai didi

c# - 在 asp.net core 中读取配置并将其加载到自定义对象中时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:20 25 4
gpt4 key购买 nike

我试图读取配置并填充自定义对象,但我只得到 LCTools 而不是 LCLog

我尝试使用 .GetSection("LCTools:LCLog")

appsettings.json:

{   
"LCTools": {
"LCLog": {
"AppId": 1
}
}
}

我在哪里读取和加载配置:

public static IApplicationBuilder UseLCLog(this IApplicationBuilder builder, IConfiguration configuration)
{
ILCToolsOptions options = configuration.GetSection("LCTools").GetSection("LCLog").Get<_LCToolsOptions>();
LogManager.Configuration = LCLog.Utility.GetConfiguration(options);
ILoggerFactory factory = builder.ApplicationServices.GetRequiredService<ILoggerFactory>();

factory.AddNLog();
factory.AddProvider(new DbLCLogProvider(options.LCLogOptions.Filter, configuration));

return builder;
}

:

public interface ILCLogBase
{
Func<string, LogLevel, bool> Filter { get; set; }
}

public abstract class LCLogBase : ILCLogBase
{
public Func<string, LogLevel, bool> Filter { get; set; }
}

public interface ILCToolsOptions
{
ILCLogOptions LCLogOptions { get; set; }
}

public class _LCToolsOptions : LCLogBase, ILCToolsOptions
{
public ILCLogOptions LCLogOptions { get; set; }
}

public interface ILCLogOptions : ILCLogBase
{
int AppId { get; set; }
string FileName { get; set; }
string FileLogLevel { get; set; }
string DbLogLevel { get; set; }
string MicrosoftLevel { get; set; }
string ConnectionString { get; set; }
}

public class LCLogOptions : LCLogBase, ILCLogOptions
{
public int AppId { get; set; }
public string FileName { get; set; } = "${basedir}/_Log/${shortdate}.log";
public string FileLogLevel { get; set; } = "Trace";
public string DbLogLevel { get; set; } = "Warn";
public string MicrosoftLevel { get; set; } = "Trace";
public string ConnectionString { get; set; } = Utility.ConnectionString;
}

最佳答案

您的主要问题看起来像是对配置生成器如何填充选项类的误解。配置 API 在读取所有配置源时,在内部填充键值集合(简单为 Dictionary<string, string> ),其中键是带有 : 的配置参数名称用作分隔符)。对于您的 json 文件,该字典包含一项 {"LCTools:LCLog:AppId": "1"}

configuration.GetSection("LCTools").GetSection("LCLog").Get<_LCToolsOptions>();

以上代码对配置生成器的意义如下:仅使用字典中键从 LCTools:LCLog 开始的项目,获取所有可用配置并将它们映射到 _LCToolsOptions 的公共(public)属性类使用剩余的关键部分进行映射。

配置生成器不进行数据反序列化。相反,它使用不带任何参数的默认构造函数构造选项类,然后尝试使用从指定配置路径获取的键值设置该类的公共(public)属性。

而且你什么都没有因为你的LCTools:LCLog部分有AppId键值,但是 _LCToolsOptions类不提供 AppId公共(public)属性(property)。


如果您想从 LCTools:LCLog 中的数据构造选项类部分,您应该构建 LCLogOptions来自 .GetSection("LCLog") 的实例部分:

    var options = configuration.GetSection("LCTools")
.GetSection("LCLog")
.Get<LCLogOptions>();

作为 AppId 之类的属性在 LCLogOptions 中定义.


如果你想构建一个 _LCToolsOptions 的实例,那么您需要将您的选项类修改为:

public class _LCToolsOptions : LCLogBase, ILCToolsOptions
{
public ILCLogOptions LCLog { get; set; }
// instead of
// public ILCLogOptions LCLogOptions { get; set; }
}

请注意,该类型需要从接口(interface)更改为特定类,属性名称 ( LCLog) 应反射(reflect) JSON 配置文件中使用的名称。

然后做

var options = configuration.GetSection("LCTools").Get<_LCToolsOptions>();

关于c# - 在 asp.net core 中读取配置并将其加载到自定义对象中时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50071210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com