.NET Core配置系统和日志

在传统的软件开发中,我们一般把数据库连接字符串等配置项放到配置文件中去,比如.NET Framework中的Web.config文件,这样如果需要修改程序连接的数据库,我们只需要修改配置文件就可以了,但是当项目变复杂以后呢,我们不能每次都去修改对应的配置文件吧,这样太复杂了…


前言

今天所记录的就是为了解决这个问题的,.NET Core的配置系统,而且夺取配置有很多方式


一、基本使用

我们现在熟悉一下配置系统的基本使用:
1,我们先在根目录下添加一个JSON文件config.json

在这里插入图片描述
2,安装基础开发包Microsoft.Extensions.ConfigurationMicrosoft.Extensions.Configuration.Json

  • NuGet解决方案位置

第二个
在这里插入图片描述
然后在浏览里找到搜索框,依次输入

Microsoft.Extensions.Configuration
在这里插入图片描述

3,接下来就是读取配置的代码

ConfigurationBuilder configBuilder = new ConfigurationBuiler();
//向ConfigurationBuilder添加了一个待解析的配置文件,optional参数表示这个文件是否可选,reloadOnChange参数表示如果文件修改了,是否重新加载配置。
configBuilder,AddJsonFile("config.json",optional: false,reloadOnchange:false);
//构建IConfigurationRoot对象,我们能够通过它读取配置项,如果配置分级,也可以用“proxy:address”这种冒号分隔的方式读取配置项。
IConfigurationRoot config = configBuilder.Builder();
string name = config("name");
string proxyAddress = config.GetSection("proxy:address").Value;
Console.WriteLine($"Address:{proxyAddress}");

需要注意的是,IConfigurationRoot中有一个GetConnectionString(string name)方法用于获取连接字符串,它读取“ConnectionStrings”节点下的名为name的值作为连接字符串。“ConnectionStrings”只是一个建议,不是.NET Core要求必须使用这个节点保存数据库连接字符串。

选项方式读取配置(推荐)

第一步,还是安装包
在这里插入图片描述
在这里插入图片描述
第二步,建配置文件 appsettings.json,模型类

{
  "Logging": { "LogLevel": { "Default": "Warning" } },
  "DB": {
    "DbType": "SQLServer",
    "ConnectionString": "Data Source=.;Initial Catalog=DemoDB; Integrated Security=True"
  },
  "Smtp": {
    "Server": "smtp.youzack.com",
    "UserName": "zack",
    "Password": "hello888"
  },
  "AllowedHosts": "*"
}

模型类

public class DbSettings
{
    public string DbType { get; set; }
    public string ConnectionString { get; set; }
}
public class SmtpSettings
{
    public string Server { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
}

用于测试读取配置的Demo类
在这里插入图片描述
注入服务到容器

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

ConfigurationBuilder configBuilder = new ConfigurationBuilder();
configBuilder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
IConfigurationRoot config = configBuilder.Build();
ServiceCollection services = new ServiceCollection();
services.AddOptions()
    .Configure<DbSettings>(e => config.GetSection("DB").Bind(e))
    .Configure<SmtpSettings>(e => config.GetSection("Smtp").Bind(e));
services.AddTransient<Demo>();
using (var sp = services.BuildServiceProvider())
{
    while (true)
    {
        using (var scope = sp.CreateScope())
        {
            var spScope = scope.ServiceProvider;
            var demo = spScope.GetRequiredService<Demo>();
            demo.Test();
        }
        Console.WriteLine("可以改配置啦");
        Console.ReadKey();
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

栀梦星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值