NLog: Logs in wrong file on reload - c#

We have a config file that is edited by our GUI to dynamically change the log level of our file target.
Our filename looks like this: fileName="${logDir}${var:subLogDir}/${var:regionName}${logName}.dlog"
All of the variables are in the base .config file of the program and the regionName is set dynamically when the program starts by setting
variable directly in the configuration via NLog.LogManager.Configuration.Variables["regionName"] = string.Concat(RegionName, "/");.
Our problem is when we modify the log level while the program is generating logs, there will be a moment where the logs will fall into the ${var:subLogDir} directly as if the ${var:regionName} is not set even if we know it is.
It seems like there is a moment in time where the dynamic variables are not resolved yet. Is there something we can do to prevent this? Buffering the logs while reloading?
NLog version: 4.7.4
.config file
<nlog keepVariablesOnReload="true">
<variable name="subLogDir" value="/Regions" />
<include file="..\Common\Logs\BaseVariables.xml" />
<include file="..\Common\Logs\BaseTargets.xml" />
<include file="..\Common\Logs\BaseRules.xml" />
</nlog>
BaseVariables.xml
<nlog>
<!-- Default variables values -->
<variable name="logLevel" value="LogLevel.Info"/>
<variable name="callstackLogLevel" value="LogLevel.Debug"/>
<variable name="keepFileOpen" value="true"/>
<variable name="autoFlush" value="true"/>
<variable name="concurrentWrites" value="false"/>
<variable name="hasAudit" value="false"/>
<variable name="archiveAboveSize" value="52428800"/>
<variable name="maxArchiveDays" value="365"/>
<variable name="archiveNumbering" value="DateAndSequence"/>
</nlog>
BaseTargets.xml
<nlog>
<targets async="true">
<target Type="File" name="LogFile" createDirs="true" keepFileOpen="${keepFileOpen}" autoFlush="${autoFlush}"
fileName="${logDir}${var:subLogDir}/${var:regionName}${logName}.dlog"
concurrentWrites="${concurrentWrites}"
cleanupFileName="false"
archiveFileName="${archiveLogDir}${var:subLogDir}/${var:regionName}${archiveLogName}.dlog"
archiveEvery="${archiveEvery}" archiveAboveSize="${archiveAboveSize}"
archiveNumbering="${archiveNumbering}" archiveDateFormat="${archiveDateFormat}" maxArchiveDays="${maxArchiveDays}">
<layout Type="CSVLayout" delimiter="Tab" >
<column name="date" layout="${longdate}" />
<column name="level" layout="${uppercase:${level}}" />
<column name="source,keywords" layout="${logger}" />
<column name="message" layout="${message}" />
</layout>
</target>
</targets>
</nlog>
Thanks

You want the NLog Config Variables to behave in two ways:
When editing the NLog.config file, then it should discard the existing NLog Config Variables and take those when reloading the updated NLog.config.
This requires that you have configured KeepVariablesOnReload=false (default)
It would be easier if you had included NLog.config in your question. Instead of having to guess.
After the reload of the NLog.config then there is one NLog Config Variable (${var:regionName}) that you would like to keep.
Because you have configured KeepVariablesOnReload=false (default) then I guess you have hooked into LogManager.ConfigurationReloaded-event and re-assigning it manually.
It would be easier if you had included the logic for dynamic assigment of NLog config variable in your question. Instead of having to guess.
The LogManager.ConfigurationReloaded-event is called after the reloaded NLog Config has been assigned and initialized. So any logging happening until having dynamically re-assigned the ${var:regionName} will get empty value. See also https://github.com/NLog/NLog/pull/3954 and https://github.com/NLog/NLog/pull/3952
The current work-around is stop using LogManager.ConfigurationReloaded-event and instead just use NLog Global Diagnostic Context (GDC) for the single NLog Config Variable that needs to be restored on every reload. So it becomes ${gdc:regionName} and you assign it like this (once at startup):
NLog.GlobalDiagnosticsContext.Set("regionName", "Europe");
See also: https://github.com/NLog/NLog/wiki/Gdc-layout-renderer

Related

Format JSON object in Console or File targets of NLog

For a simple prototype program, I am using NLog with ColoredConsole and File targets, with simple layouts (see configuration file below).
And I am using # operator to destructure objects :
Logger.Debug("values : {#result}", aRandomObject);
where aRandomObject could be anything.
Unfortunately, for both the Console and File targets, by default the destructured object is rendered in json without any formatting. Pretty convienient for computers, but much harder to read for humans.
Instead, I would like to find a way to print my objects as formatted json (with indentations and line breaks) if it's possible with Nlog.
I don't want to render the whole target as a json (as a "JsonLayout" allows), but only the objects logged with #.
Here's the full NLog configuration :
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="true"
internalLogLevel="Off" internalLogFile="c:/temp/nlog-internal.log">
<variable name="baseLogDirectory" value="C:\Logs\Thing"/>
<targets>
<target xsi:type="File" name="file" fileName="${baseLogDirectory}\${cached:cached=true:inner=${date:format=yyyyMMdd_HHmmss}}_${processid}.log"
layout="${longdate} ${uppercase:${level}} ${callsite} ${message} ${exception:format=toString,Data}" />
<target name="console" xsi:type="ColoredConsole" layout="${longdate}|${pad:padding=5:inner=${level:uppercase=true}}|${message}" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file, console" />
</rules>
</nlog>
Thank you

Specflow - How to get Target Name from srprofile programatically in C#

Is there any way to get the Target name defined in specflow's srs profile ?
Below is what is defined in srs profile,
<Targets>
<Target name="Login">
<Filter>#login</Filter>
</Target>
</Targets>
I want to get the name of the target "Login" and save it in a variable in C#.
We don't have an API to get the target name, but you can get to it with a little bit more stuff in the srProfile.
This is how you do it:
<?xml version="1.0" encoding="utf-8"?>
<TestProfile xmlns="http://www.specflow.org/schemas/plus/TestProfile/1.5">
<Targets>
<Target name="Target1">
<DeploymentTransformationSteps>
<EnvironmentVariable variable="RUNNER_TARGET" value="Target1" />
</DeploymentTransformationSteps>
</Target>
<Target name="Target2">
<DeploymentTransformationSteps>
<EnvironmentVariable variable="RUNNER_TARGET" value="Target2" />
</DeploymentTransformationSteps>
</Target>
</Targets>
</TestProfile>
You specify a deployment transformation step which sets an environment variable to the name of the target.
In your bindings, you can get the value of the environment variable by normal .NET APIs.
var targetName = Environment.GetEnvironmentVariable("RUNNER_TARGET");
You can find a complete example here: https://github.com/SpecFlowOSS/SpecFlow.Plus.Examples/tree/master/AccessTargetName
Full disclosure: I am one of the developers of SpecFlow and SpecFlow+

NLOG: how to use variable to specify target

Is it possible to use a variable to specify a specific target?
I want to switch between database and filelogging depending on the environment i'm running my application on.
This however does not seem to work:
<logger name="MyLogger" minlevel="Warn" writeTo="${var:myTargetName}" />
In my application startup I have (error occurs on first line):
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
var config = LoadConfiguration();
NLog.LogManager.Configuration.Variables["myTargetName"] = config.GetSection("LogTargets:TargetName").Value;
NLog.LogManager.KeepVariablesOnReload = true;
NLog.LogManager.Configuration.Reload();
When startin the application the following exception is thrown:
"Target ${var:myTargetName} not found."
I guess the variables are not available when parsing the config file.
How can I set the variables so NLOG uses them while parsing the config-file?
Is it possible to use a variable to set an attribute value?
Or is this not supported by NLOG?
Note: I do have another variable in the NLOG config which does work
<target xsi:type="Database" name="databaseTarget">
<connectionString>${var:connectionString}</connectionString>
...........
</target>
I guess the loggers are checked once on startup and the actual target is evaluated when a logevent occurs.
Update: got it working without variable in config file.
I had to remove the logger from the config and create it through code
var myCustomTarget = NLog.LogManager.Configuration.FindTargetByName(config.GetSection("LogTargets:TargetName").Value);
NLog.LogManager.Configuration.AddRuleForAllLevels(myCustomTarget , "MyLogger", true);
One way to do it is to have a different Web.config file based on the environment and there you will change the connection string. That is the method I am using.
Also NLOG is initialized once the application starts thus you cannot change what's written in the NLOG.config
Setting variables via NLog.LogManager.Configuration.Variables does not work for all attributes in nlog.config file. I don't know why, but that's a known problem and that is unfortunate how it works. But there is a simple workaround, here is an example how I solved this problem for the attribute connectionString of a target.
<target xsi:type="Database" name="tDatabase"
dbProvider="Microsoft.Data.Sqlite.SqliteConnection, Microsoft.Data.Sqlite"
connectionString="Data Source =.\${environment:DATABASE_FILE_NAME};"
commandText="INSERT INTO ...">
<parameter name="#MachineName" layout="${machinename}" />
...
</target>
You can than simple set the environment variable in your code like this
Environment.SetEnvironmentVariable("DATABASE_FILE_NAME", "foo.db");
So you just use ${environment:DATABASE_FILE_NAME} and
Environment.SetEnvironmentVariable("DATABASE_FILE_NAME", "foo.db");
instead of ${var:DATABASE_FILE_NAME} and
NLog.LogManager.Configuration.Variables["DATABASE_FILE_NAME"] = "foo.db";
NLog 4.6.7 makes it easier to update the logging-rules at runtime like this:
<nlog>
<variable name="fileMinLevel" value="Off" />
<variable name="databaseMinLevel" value="Off" />
<rules>
<logger minLevel="${var:fileMinLevel}" writeTo="fileTarget" />
<logger minLevel="${var:databaseMinLevel}" writeTo="databaseTarget" />
</rules>
</nlog>
Then you can do this:
if (IsDevelopment())
LogManager.Configuration.Variables["fileMinLevel"] = "Debug";
else
LogManager.Configuration.Variables["databaseMinLevel"] = "Debug";
LogManager.ReconfigExistingLoggers();
See also: https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules
See also: https://github.com/NLog/NLog/wiki/Environment-specific-NLog-Logging-Configuration

NLog - Archive Start / Stop Time

Just started working with NLog and have it running with the following configuration:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwConfigExceptions="true">
<targets async="true">
<target name="logfile"
xsi:type="File"
layout="${longdate} [${level:uppercase=true}] (${threadid}) ${logger}: ${message} ${onexception:${newline}Exception\: ${exception:format=type,message,method,stacktrace:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}}"
fileName="logs/current.log"
archiveFileName="logs/Archive/${ticks}.log"
archiveEvery="Minute"
archiveOldFileOnStartup="true"
keepFileOpen="false"
/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
Everything is working as expected. However, I need to have the rotated file be in the format of ${archive_start_ticks}_${arhive_end_ticks}.log rather than the current format which is ${archive_end_ticks}.log.
I was initially hoping I could name the active log file as ${ticks} and then, on archive, use the active log file's name as a parameter into the archive file to compose some like:
fileName="logs/${ticks}"
archiveFileName="logs/Archive/${fileName}_${ticks}.log"
Of course, there's two issues here:
Using ${ticks} for the active file creates a new file for each log line.
I can't seem to reference the original fileName as an input variable into archiveFileName.
That said, what is the best way to achieve this goal? Is this something NLog can handle natively or with minor extensions?
Updating in case anyone ever cares:
I bailed on using the FileTarget with configurations and wrote my own Target wrapped in a BufferedWrapper. On each flush, I use the first and last LogEvents to determine the timespan which gives me what I need to for the required file format with little custom code to support.

Why does NLog miss some messages when logging a large number of messages?

I try to test NLog performance (latest version) with settings:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
<variable name="basePath" value="c:\logs\" />
<variable name="msgFormat" value="${message}" />
<targets async="true">
<target name="file"
xsi:type="File"
fileName="${basePath}/${logger}/${date:format=yyyy}/${date:format=MMMM}/log-${date:format=yyMMdd}-${level}.log"
layout="${msgFormat}"
concurrentWrites="true" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file"/>
</rules>
</nlog>
and run this code:
var msg = "this is example string for logging test. it's not very long, but not very short";
var count = 20000;
Parallel.For(0, count, x => nlog.Info(msg));
NLog writes to file, but when file size reaches 1MB it stops writing. I try to use simple for loop, but it doesn't helped me.
And i try to use internal logging, but there is no errors, by the way i see there this strings:
2013-04-01 11:36:18.2458 Trace Opening
c:\logs/NLogTest/2013/April/log-130401-Info.log with
concurrentWrite=False
It's very strange, because concurrentWrites default value is true, furthermore I've set this value in config.
The problem lies in the default value of the AsyncWrappers QueueLimit, which is 10000.
The value determines how big the queue of messages to write are allowed to be, the problem arises because all 20000 messages are queued before anything is written to the file, which causes NLog to discard the last 10000 messages.
Unfortunately this cannot be changed when using the async attribute, you have to define the AsyncWrapper manually to be able to control the QueueLimit, which is done like this:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
<variable name="basePath" value="c:\logs\" />
<variable name="msgFormat" value="${message}" />
<targets async>
<target name="asyncWrapper" xsi:Type="AsyncWrapper" queueLimit="20000">
<target name="file"
xsi:type="File"
fileName="${basePath}/${logger}/${date:format=yyyy}/${date:format=MMMM}/log-${date:format=yyMMdd}-${level}.log"
layout="${msgFormat}"
concurrentWrites="true" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file"/>
</rules>
</nlog>
Where QueueLimit is set to 20000.
You could also changed the OverflowAction if you need to do something other the discard messages not put in the queue, see AsyncWrapper documentation for more information. The options are Block, Discard or Grow.

Categories

Resources