• Set Serilog minimum level from environment variable


    Set Serilog minimum level from environment variable

    问题

    Is it possible to setup Serilog minimum log level from environment variable?

    If I try to configure it like this

      "Serilog": {
        "MinimumLevel": "%LOG_LEVEL%",
        "WriteTo": [
          {
            "Name": "RollingFile",
            "Args": {
              "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [v{SourceSystemInformationalVersion}] {Message}{NewLine}{Exception}",
              "pathFormat": "%LOG_FOLDER%/sds-osdr-domain-saga-host-{Date}.log",
              "retainedFileCountLimit": 5
            }
          }
        ]
      }

    it returns error

    The value %LOG_LEVEL% is not a valid Serilog level.

    Is it possible to propagate log level from environment variable somehow?

    回答

    I think you are asking about configuration by environment which is not specific to serilog.

    If the LOG_LEVEL is fixed with the specific environment (development, staging or production), you can set the each LOG_LEVEL in appsettings.<EnvironmentName>.json, and set configuration like this:

    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .Build();

    If you need to config the LOG_LEVEL from environment variable in docker-compose file or kubernetes deployment file, then you can read values from environment variables by calling AddEnvironmentVariables:

    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false)
        .AddEnvironmentVariables()
        .Build();

    And set the environment Serilog:MinimumLevel in windows or ``Serilog__MinimumLevel` in linux and mac.

    serilog / serilog-settings-configuration

    After installing this package, use ReadFrom.Configuration() and pass an IConfiguration object.

    static void Main(string[] args)
    {
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
            .Build();
    
        var logger = new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
            .CreateLogger();
    
        logger.Information("Hello, world!");
    }

    This example relies on the Microsoft.Extensions.Configuration.Json, Serilog.Sinks.Console, Serilog.Sinks.File, Serilog.Enrichers.Environment and Serilog.Enrichers.Thread packages also being installed.

    For a more sophisticated example go to the sample folder.

     

     

  • 相关阅读:
    Hypercall机制
    python 基础-----数字,字符串,if while 循环 数据类型的转换简单介绍
    计算机基础知识
    Proxmox初步了解
    Centos7-安装py3
    KVM-virsh常用命令
    Centos7-VNC安装
    Centos7-bond模式介绍
    KVM管理工具
    Win10-无法启动虚拟机
  • 原文地址:https://www.cnblogs.com/chucklu/p/16590752.html
Copyright © 2020-2023  润新知