3

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.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
jackfrost5234
  • 229
  • 5
  • 12
  • Please be more specific about what error is being thrown. – Frost2779 Jul 09 '19 at 14:18
  • The error message you quoted in your question gives you plenty of advice how you can further troubleshoot the issue. Have you actually read the error message there before pasting it into your question? If you already have followed those troubleshooting tips, what did you discover? –  Jul 09 '19 at 14:22
  • Added some more details as I try to troubleshoot it. – jackfrost5234 Jul 09 '19 at 14:28
  • have you tried to follow this advice: "Enable logging the application process' stdout messages" ? – alexm Jul 09 '19 at 23:55

2 Answers2

5

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

        });
Ryan
  • 19,118
  • 10
  • 37
  • 53
0

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.

user2554240
  • 171
  • 1
  • 5