在Program.cs中阅读和使用appsettings.json? [英] Reading and using appsettings.json in Program.cs?
问题描述
我正在尝试为我的应用配置日志记录以使用指定的路径,但是我尝试访问Program.cs文件中的appsettings.json似乎不起作用.它将引发错误,并且应用程序无法启动.
我发现了:
在主Program.cs中阅读appsettings.json >
尝试了其中的建议,但似乎没有用.
我的Program.cs文件:
公共类程序{公共静态无效的Main(string [] args){var config = new ConfigurationBuilder().AddJsonFile("appsettings.json",可选:false).建造();LogConfig logConfig =新的LogConfig();config.GetSection("Config").Bind(logConfig);CreateWebHostBuilder(args,logConfig).Build().Run();}公共静态IWebHostBuilder CreateWebHostBuilder(string [] args,LogConfig配置)=>WebHost.CreateDefaultBuilder(args).ConfigureLogging(builder => builder.AddFile(options => {options.FileName ="AppLog-";//日志文件前缀options.LogDirectory = config.LoggingPath;//写入日志的目录options.FileSizeLimit = 20 * 1024 * 1024;//最大日志文件大小(此处为20MB)options.Extension ="txt";//日志文件扩展名options.Periodicity = PeriodicityOptions.Hourly;//每小时(而不是每天)滚动日志文件.})).UseStartup< Startup>();.}
和LogConfig.cs:
公共类LogConfig{私有字符串loggingPath;公共字符串LoggingPath {get =>loggingPath;设置=>loggingPath =值;}}
当我尝试启动我的应用程序时,出现以下错误消息:
HTTP错误500.30-ANCM进程内启动失败此问题的常见原因:应用程序无法启动应用程序启动但随后停止该应用程序已启动,但在启动过程中引发了异常故障排除步骤:检查系统事件日志中是否有错误消息启用记录应用程序进程的标准输出消息将调试器附加到应用程序进程并检查有关更多信息,请访问: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)
来获取它
公共静态IWebHostBuilder CreateWebHostBuilder(string [] args)=>WebHost.CreateDefaultBuilder(args).UseStartup< Startup>().ConfigureLogging((hostingContext,builder)=>{var loggingPath = HostingContext.Configuration.GetSection("Config");builder.AddFile(options =>options.LogDirectory = loggingPath;//...);});
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 ;
//...
);
});
这篇关于在Program.cs中阅读和使用appsettings.json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!