Reputation: 3727
I'm working on my first .Net Core website in Azure and I'm using NLog along with blob storage. I've got that working but the connection string is hard coded in the NLog.config file right now. I would like to move the connection string into the appsettings.json file so that I can change it for either Dev/Stage environments based on where I'm publishing it to in my CI/CD pipeline. I found the following article that uses Azure Key Vault for the connection string and eventually we will start using Key Vault but we are not at that point just yet. The function that I need to use is the UpdateNLogConfig in the article where I'll pull the connection from appsettings.json and then set it to the GDC which I will then retrieve from the NLog.config file. What I'm not sure is when/where do I call the UpdateNLogConfig.
This is what I have in my Program.cs so far. I know I'll need to add the UpdateNLogConfig()
here as well.
public class Program
{
public static void Main(string[] args)
{
//var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
CreateHostBuilder(args).Build().Run();
}
catch (Exception exception)
{
//logger.Error(exception, "Stopped program because of exception.");
throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
NLog.LogManager.Shutdown();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.UseNLog();
}
UPDATE:
I'm working on converting over to setting the NLog configuration in my appsettings.json
instead of using NLog.config
file. I have followed the threads suggested and came up with the following. It will write out to the internal-nlog.txt
file but I can't get it to create or write out to my LocalTarget. I feel like it must be installed and configured because it's writing to the internal-nlog.txt but I can's see what would be causing it to not write to my LocalTarget.
Program.cs
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
NLog.Config.LoggingConfiguration nlogConfig = new NLogLoggingConfiguration(config.GetSection("NLog"));
var logger = NLog.Web.NLogBuilder.ConfigureNLog(LogManager.Configuration).GetCurrentClassLogger();
try
{
logger.Debug("Initialize main");
CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
logger.Error(ex, "Stopped program because of exception.");
//throw;
}
finally
{
NLog.LogManager.Shutdown();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.UseNLog(); // NLog: Setup NLog for Dependency injection
}
appsettings.json
"NLog": {
"internalLogLevel": "Info",
"internalLogFile": "c:\\temp\\NLog\\internal-nlog.txt",
"extensions": {
"NLog.Web.AspNetCore": {
"assembly": "NLog.Web.AspNetCore"
}
},
"targets": {
"LocalTarget": {
"type": "File",
"fileName": "c:\\temp\\NLog\\PayDocs_${shortdate}.log",
"layout": "${longdate} | ${uppercase:${level}} | ${callsite} | ${message}"
},
"rules": [
{
"logger": "*",
"minLevel": "Trace",
"writeTo": "LocalTarget"
},
{
"logger": "Microsoft.*",
"maxLevel": "Info",
"final": "true"
}
]
}
Upvotes: 1
Views: 1158
Reputation: 22039
UpdateNLogConfig
just load nlog configuration, and it's a custom method.
You can load your configuration from appsettings.json
by below code.
var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
NLog.Config.LoggingConfiguration nlogConfig = new NLogLoggingConfiguration(config.GetSection("NLog"));
*For more details, you can refer below related post. *
How can I configure NLog in .NET Core with appsettings.json instead of an nlog.config file?
Upvotes: 2