简体   繁体   中英

Reading and using appsettings.json in Program.cs?

I'm trying to configure the logging for my app to use a specified path, but my attempts to access appsettings.json in my Program.cs file don't seem to be working. It throws an error and the application doesn't start.

I found this:

Read appsettings.json in Main Program.cs

and tried the advice therein, but doesn't seem to be working.

My Program.cs file:

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>();
}

And LogConfig.cs:

public class LogConfig
{
    private string loggingPath;

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

When I try to start my app, I get the following error message thrown:

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

Checked the system event log, this seems to be the exact error message:

Application '/LM/W3SVC/2/ROOT' with physical root 'C:\\App\\CatalogManager\\' failed to load clr and managed application. CLR worker thread exited prematurely

There was also this:

Application '/LM/W3SVC/2/ROOT' with physical root 'C:\\App\\CatalogManager\\' hit unexpected managed exception, exception code = '0xe0434352'. Please check the stderr logs for more information.

You could directly get it use 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 ; 
               //...
            );

        });

Old post but my problem was not setting my appsettings.json file to content and copy if newer. The app couldn't find my file to load my settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM