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.
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();
}
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