【问题标题】:Reading and using appsettings.json in Program.cs?在 Program.cs 中读取和使用 appsettings.json?
【发布时间】:2019-11-19 03:08:24
【问题描述】:

我正在尝试将我的应用程序的日志记录配置为使用指定的路径,但我尝试访问我的 Program.cs 文件中的 appsettings.json 似乎不起作用。它引发错误并且应用程序无法启动。

我发现了这个:

Read appsettings.json in Main Program.cs

并尝试了其中的建议,但似乎没有用。

我的 Program.cs 文件:

public class Program
{
    public static void Main(string[] args)
    {

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

        LogConfig logConfig = new LogConfig();
        config.GetSection("Config").Bind(logConfig);
        CreateWebHostBuilder(args, logConfig).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args, LogConfig config) =>
        WebHost.CreateDefaultBuilder(args)
        .ConfigureLogging(builder => builder.AddFile(options => {
            options.FileName = "AppLog-"; // The log file prefixes
            options.LogDirectory = config.LoggingPath; // The directory to write the logs
            options.FileSizeLimit = 20 * 1024 * 1024; // The maximum log file size (20MB here)
            options.Extension = "txt"; // The log file extension
            options.Periodicity = PeriodicityOptions.Hourly; // Roll log files hourly instead of daily.
        }))
            .UseStartup<Startup>();
}

还有 LogConfig.cs:

public class LogConfig
{
    private string loggingPath;

    public string LoggingPath { get => loggingPath; set => loggingPath = value; }
}

当我尝试启动我的应用程序时,我收到以下错误消息:

HTTP Error 500.30 - ANCM In-Process Start Failure
Common causes of this issue:
The application failed to start
The application started but then stopped
The application started but threw an exception during startup
Troubleshooting steps:
Check the system event log for error messages
Enable logging the application process' stdout messages
Attach a debugger to the application process and inspect
For more information visit: https://go.microsoft.com/fwlink/?LinkID=2028265

检查了系统事件日志,这似乎是确切的错误消息:

物理根目录为“C:\App\CatalogManager\”的应用程序“/LM/W3SVC/2/ROOT”无法加载 clr 和托管应用程序。 CLR 工作线程提前退出

还有这个:

物理根目录为“C:\App\CatalogManager\”的应用程序“/LM/W3SVC/2/ROOT”遇到意外托管异常,异常代码 =“0xe0434352”。请查看 stderr 日志以获取更多信息。

【问题讨论】:

  • 请更具体地说明引发了什么错误。
  • 您在问题中引用的错误消息为您提供了如何进一步解决问题的大量建议。在将错误消息粘贴到您的问题之前,您是否真的阅读过那里的错误消息?如果您已经按照这些故障排除提示进行操作,您发现了什么?
  • 在我尝试对其进行故障排除时添加了更多详细信息。
  • 您是否尝试过遵循以下建议:“启用记录应用程序进程的标准输出消息”?

标签: c# asp.net-core


【解决方案1】:

你可以直接使用ConfigureLogging(hostcontext,builder)like

 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
         .UseStartup<Startup>()
        .ConfigureLogging((hostingContext, builder) =>
        {
            var loggingPath = hostingContext.Configuration.GetSection("Config");
            builder.AddFile(options=>
               options.LogDirectory = loggingPath ; 
               //...
            );

        });

【讨论】:

    【解决方案2】:

    旧帖子,但我的问题是没有将我的 appsettings.json 文件设置为内​​容并在更新时复制。该应用找不到我的文件来加载我的设置。

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 2021-11-22
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      相关资源
      最近更新 更多