Dynamically Set NLog Levels in Code

NLog variable to set the log level on startup in .NET / .NET Core

NLog Configuration with Variables

A little background here. We have a few containerized application components that we are working on for OpenRMF Professional. We wanted to allow customers to make logs more or less verbose to help us help them during remote disconnected network troubleshooting. By default we report Critical/Fatal, Error and Warning. However, with this setup discussed here we can allow them to alter that level for various reasons.

<!-- rules to map from logger name to target -->
<rules>
<logger name="Microsoft.*" maxLevel="${var:logLevel}" final="true" />
<logger name="*" minlevel="${var:logLevel}" writeTo="Console" />
<logger name="*" minlevel="${var:logLevel}" writeTo="Logstash" />
</rules>

Setting Your NLog Level at Startup

We use the NuGet packages below to reference in our API and NATS message client code. The NLog.Web is a little different setup than regular NLog reading in the nlog.config file. They are pretty similar though. Check the NLog website for configuration information. The NuGet packages are the same, minus the Web.AspNetCore one.

<PackageReference Include="NLog" Version="4.7.13" />
<PackageReference Include="NLog.StructuredLogging.Json" Version="4.0.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.14.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.7.4" />
"env" : {
"LOGLEVEL" : "3"
},
var logger = 
NLog.Web.NLogBuilder.ConfigureNLog("nlog.config")
.GetCurrentClassLogger();
if (string.IsNullOrEmpty(
Environment.GetEnvironmentVariable("LOGLEVEL")))
NLog.LogManager.Configuration.Variables["logLevel"]
= "Warn";
else {
switch (Environment.GetEnvironmentVariable("LOGLEVEL"))
{
case "5":
NLog.LogManager.Configuration.Variables["logLevel"] = "Critical";
break;
case "4":
NLog.LogManager.Configuration.Variables["logLevel"] = "Error";
break;
case "3":
NLog.LogManager.Configuration.Variables["logLevel"] = "Warn";
break;
case "2":
NLog.LogManager.Configuration.Variables["logLevel"] = "Info";
break;
case "1":
NLog.LogManager.Configuration.Variables["logLevel"] = "Debug";
break;
case "0":
NLog.LogManager.Configuration.Variables["logLevel"] = "Trace";
break;
default:
NLog.LogManager.Configuration.Variables["logLevel"] = "Warn";
break;
}
}
// reset based on the variable passed in
NLog.LogManager.ReconfigExistingLoggers();

Test Your NLog Configuration

Now that you have your environment variable set, your nlog.config set, and your Program.cs Main() code done you can test it and tweak your code and documentation accordingly. If you are using VS Code like we do, you may need to copy your nlog.config into your bin/Debug/net6.0/ directory possibly to make your code read in the file properly in debug sessions. Once you are set, debug or run and try different levels.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dale Bingham

Dale Bingham

CEO of Soteria Software. Developer on OpenRMF. Software Geek by trade. Father of three daughters. Husband. Love new tech where it fits. Follow at @soteriasoft