c# - 在 Program.cs 中读取和使用 appsettings.json?

标签 c# asp.net-core

我正在尝试为我的应用程序配置日志记录以使用指定路径,但我尝试访问我的 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 日志以获取更多信息。

最佳答案

你可以直接使用 ConfigureLogging(hostcontext,builder) 来获取它

 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 ; 
               //...
            );

        });

关于c# - 在 Program.cs 中读取和使用 appsettings.json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56954607/

相关文章:

c# - 使用 MVC 过滤器的国际化问题

c# - 在 C# 中使用身份验证复制文件

c# - VS 2012 在错误的线程上中断

asp.net-core - Asp.net Core 中内存使用的限制

ASP.NET Core 错误 : The JSON value could not be converted to System. 字符串

c# - 在 64 位 Office 插件中加载 32 位 COM dll

c# - 类似于字典的数据结构,但有范围?

c# - 从Entity Framework Core调用 native SQL Server函数

c# - 找不到与命令 "dotnet-/app/Build\ClearPluginAssemblies.dll"Docker 匹配的可执行文件

c# - InvalidOperationException Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext bindingContext)