Serilog配置读取根据.NET 5中的Worker Service中的环境变量读取

发布于 2025-01-23 05:17:24 字数 1495 浏览 0 评论 0原文

根据环境变量,我对Serilog有不同的配置。它们写在appsettings.json and appsetting.development.json中。我的问题是在配置Logger时,即使在开发模式下,也不会从AppSettings.development.json读取。

我尝试过这样的配置,

.ConfigureAppConfiguration((w, c) =>
      {
          var env = w.HostingEnvironment;
          if (env.EnvironmentName == "Development")
          {
               c.AddCustomJsonFile($"appsettings.{env.EnvironmentName}.json");
          }
          else
          {
               c.AddCustomJsonFile($"appsettings.json");
          }
     })
.ConfigureServices((hostContext, services) =>
      {
         Log.Logger = new LoggerConfiguration()
             .ReadFrom.Configuration(hostContext.Configuration)
             .CreateLogger();
      }).UseSerilog();

而我的appsettings.json就是这样,

"Serilog": {
"MinimumLevel": {
  "Defult": "Information",
  "Override": {
    "Micorsoft": "Warning",
    "System": "Information"
  }
},
"WriteTo": [
  { "Name": "Console" },
  {
    "Name": "Seq",
    "Args": {
      "serverUrl": "http://localhost:5341/"
    }
  }
]

在appsetting.development.json中,

"Serilog": {
"MinimumLevel": {
  "Defult": "Debug",
  "Override": {
    "Micorsoft": "Warning",
    "System": "Information"
  }
},
"WriteTo": [
  { "Name": "Console" },
  {
    "Name": "Seq",
    "Args": {
      "serverUrl": "http://localhost:5341/"
    }
  }
]

我已经使用其他服务对其进行了测试,它使用了基于环境变量的正确appsettings,只有Seri​​log就是有问题。有人知道如何解决这个问题吗? 提前致谢。

I have different configurations for Serilog based on the Environment variable. And they are written in the appsettings.json and appsetting.Development.json. My problem is when configuring logger it does not read from the appsettings.Development.json even in Development mode.

I've tried to configure it like this,

.ConfigureAppConfiguration((w, c) =>
      {
          var env = w.HostingEnvironment;
          if (env.EnvironmentName == "Development")
          {
               c.AddCustomJsonFile(
quot;appsettings.{env.EnvironmentName}.json");
          }
          else
          {
               c.AddCustomJsonFile(
quot;appsettings.json");
          }
     })
.ConfigureServices((hostContext, services) =>
      {
         Log.Logger = new LoggerConfiguration()
             .ReadFrom.Configuration(hostContext.Configuration)
             .CreateLogger();
      }).UseSerilog();

And my appsettings.json is like this,

"Serilog": {
"MinimumLevel": {
  "Defult": "Information",
  "Override": {
    "Micorsoft": "Warning",
    "System": "Information"
  }
},
"WriteTo": [
  { "Name": "Console" },
  {
    "Name": "Seq",
    "Args": {
      "serverUrl": "http://localhost:5341/"
    }
  }
]

And in appsetting.Development.json,

"Serilog": {
"MinimumLevel": {
  "Defult": "Debug",
  "Override": {
    "Micorsoft": "Warning",
    "System": "Information"
  }
},
"WriteTo": [
  { "Name": "Console" },
  {
    "Name": "Seq",
    "Args": {
      "serverUrl": "http://localhost:5341/"
    }
  }
]

I've tested it with other services where it uses the correct appsettings based on the environment variable, Just Serilog is having problems with this. Does anybody know how to solve this?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文