Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

368
Views
Read NLog configuration from database instead of config file

is it possible to read NLog .NET configurations from database? My need is to rapidally change the severity of targets rule's, so I'm wondering if there is a way to do that. For example, if in a configuration file I have this:

<rules>
  <logger name="*" minlevel="Warning" writeTo="LogToDB" />
  <logger name="*" minlevel="Info" writeTo="LogToConsole" />
</rules>

I wanna be able to rapidally change minlevel="value" values (i.e. from database configuration table), without deploy the application configuration file.

Thanks a lots.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can reconfigure logging at runtime using APIs provided by NLog. Use below function to reconfigure logging -

public void ReconfigureLoggingLevel()
{
    foreach (var rule in NLog.LogManager.Configuration.LoggingRules)
    {
        var logLevel = GetLogLevelFromDatabase(); // your logic to fetch log level
        rule.EnableLoggingForLevel(NLog.LogLevel.FromString(logLevel));
    }
    NLog.LogManager.ReconfigExistingLoggers();
}
over 4 years ago · Santiago Trujillo Report

0

You can also do this:

<variable name="MinLevelDB" value="Warning" />
<variable name="MinLevelConsole" value="Info" />

<rules>
  <logger name="*" minlevel="${var:MinLevelDB}" writeTo="LogToDB" />
  <logger name="*" minlevel="${var:MinLevelConsole}" writeTo="LogToConsole" />
</rules>

And then you can do this:

LogManager.Configuration.Variables["MinLevelDB"] = "Debug";
LogManager.ReconfigExistingLoggers();

See also: https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!