Dynamically Set NLog Levels in Code

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

NLog Configuration with Variables

<!-- 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

<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

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Discover The Benefits of Text Mining API For Your Enterprise

The First Step After Deciding To Become A Developer And Get Your First Job

Software Engineering Bootcamp (Why I Decided to do This)

Yet Another Uni-Directional State Flow Architecture in Android

Make huge money as a programmer

Progress Update #1 (August 2)

You need to make your next project Open Source

Open source programming projects image

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

More from Medium

Observer Pattern

Common frameworks, libraries and design patterns I use

What is ‘Variables’ ? Understanding the Variables

How to deserialise a list of object from HTTP API in C# .Net Core