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)

        LogConfig logConfig = new LogConfig();
        CreateWebHostBuilder(args, logConfig).Build().Run();

    public static IWebHostBuilder CreateWebHostBuilder(string[] args, LogConfig config) =>
        .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.

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:

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.

asked on Stack Overflow Jul 9, 2019 by jackfrost5234 • edited Jul 9, 2019 by Stephen Kennedy

1 Answer


You could directly get it use ConfigureLogging(hostcontext,builder) like

 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        .ConfigureLogging((hostingContext, builder) =>
            var loggingPath = hostingContext.Configuration.GetSection("Config");
               options.LogDirectory = loggingPath ; 

answered on Stack Overflow Jul 10, 2019 by Ryan

User contributions licensed under CC BY-SA 3.0