.NET控制台读取appsettings.json,配置日志

需要安装 nuget 包 Microsoft.Extensions.Configuration 、Microsoft.Extensions.Configuration.FileExtensions 、Microsoft.Extensions.Configuration.Json、NLog

using NLog;
using NLog.Config;
using Microsoft.Extensions.Configuration;
namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            LogManager.Configuration = new XmlLoggingConfiguration(string.Format("{0}/NLog.config", AppDomain.CurrentDomain.BaseDirectory.ToString()));
            var logger = NLog.LogManager.GetCurrentClassLogger();
            logger.Error("打印日志");
            try
            {
                var a = Directory.GetCurrentDirectory();
                var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                                     .AddJsonFile("appsettings.json", true, true)
                                     .AddJsonFile("appsettings.Development.json", true, true)
                                     .Build();

                var baseUrl = config.GetSection("baseUrl").Value;
                var setting1 = config["compilerOptions:target"];
                var setting2 = config["compilerOptions:sourceMap"];
                var setting3 = config.GetSection("exclude").GetSection("1").Value;
                var setting4 = config.GetSection("user").GetSection("0").GetSection("name").Value;

                Console.WriteLine($"baseUrl: {baseUrl}");
                Console.WriteLine($"Setting1: {setting1}");
                Console.WriteLine($"Setting2: {setting2}");
                Console.WriteLine($"setting3: {setting3}");
                Console.WriteLine($"setting4: {setting4}");
            }
            catch (Exception ex) {
                logger.Error(ex.ToString());
            }
        }
    }
}
{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ],
  "baseUrl": "http://baidu.com",
  "user": [
    {
      "name": "张三",
      "code": 1
    },
    {
      "name": "李四",
      "code": 2
    }
  ]
}

 

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Warn"
      internalLogFile="c:\temp\internal-nlog.txt">

    <!-- 加载ASP.NET Core插件 -->
    <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
    </extensions>

    <!-- 输出目的地 -->
    <targets>
        <!-- 输出到文件,这个文件记录所有日志 -->
        <target xsi:type="File" name="allfile" fileName="${basedir}/logs/error-${shortdate}.txt"
                    layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />

        <!-- 另外一个日志记录文件,户口也跳过Microsoft开头相关日志信息 -->
        <target xsi:type="File" name="ownFile-web" fileName="${basedir}/logs/info-${shortdate}.txt"
                layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${exception}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action} : ${message} " />

        <!-- write to the void aka just remove -->
        <target xsi:type="Null" name="blackhole" />
    </targets>

    <!-- 写入目的地的规则 -->
    <rules>
        <!--全部记录,包括Microsoft开头的相关日志信息-->
        <logger name="*" minlevel="Error" writeTo="allfile" />

        <!--跳过Microsoft开头的相关日志信息-->
        <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
        <logger name="*" minlevel="Info" writeTo="ownFile-web" />
    </rules>
</nlog>

 

posted @ 2024-07-05 18:25  杜子烟  阅读(57)  评论(0编辑  收藏  举报