Reputation: 229
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.
Upvotes: 3
Views: 5062
Reputation: 171
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.
Upvotes: 0
Reputation: 20139
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 ;
//...
);
});
Upvotes: 5