【发布时间】:2020-12-10 22:15:13
【问题描述】:
我有一个需要添加到配置中的自定义 appsettings JSON 文件。我在从 Program.cs 中的 Main 调用的 CreateHostBuilder 调用的 ConfigureAppConfiguration 挂钩中执行此操作。我也在使用 NLog。
public static void Main(string[] args)
{
//
// currently using nlog.config but I want to use appsettings but they haven't been "built" yet
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
logger.Debug("init main");
CreateHostBuilder(args).Build().Run();
// could move configurenlog call here, but already called "UseNLog()"
}
catch (Exception exception)
{
logger.Error(exception, "Stopped program because of exception");
throw;
}
finally
{
NLog.LogManager.Shutdown();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) => {
// add custom config file
config
.AddJsonFile("customsettings.json", optional:true, reloadOnChange:true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.UseNLog(); // NLog: Setup NLog for Dependency injection
现在我想使用(组合的)appsettings 来配置 nlog as outlined here,但是在我加载了我的自定义配置设置之后我该怎么做呢?
【问题讨论】:
-
可以在控制器中生成日志吗?
标签: c# .net-core nlog appsettings