Page 1 of 1

NLOG aus Appsetings mit benutzerdefinierten Appsettings

Posted: 15 May 2025, 18:03
by Anonymous
Ich habe eine benutzerdefinierte Appsettings -JSON -Datei, die ich zur Konfiguration hinzugefügt werden muss. Ich mache dies im Konfigurations -Hook, der von CreateHostBuilder genannt wird, von Main in Program.cs. Ich benutze auch Nlog. < /P>

Code: Select all

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();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.UseNLog();  // NLog: Setup NLog for Dependency injection
Jetzt möchte ich die (kombinierten) Appsettings verwenden, um NLOG wie hier beschrieben zu konfigurieren, aber wie mache ich das, nachdem ich meine benutzerdefinierten Konfigurationseinstellungen geladen habe?