Nlog Logrotation, keep 7 days of logfiles than archive it every sunday - c#

Is there a way to create a log rotation with NLOG that records a log for each day for seven days and then archives them after the seven days?
this is my current target with this i :
<target xsi:type="File"
name="error"
layout="${longdate} ${uppercase:${level}}: - ${message}"
fileName="${basedir}/logs/Error.log"
archiveFileName="${basedir}/logs/archive/Error{#}.zip"
archiveNumbering="Rolling"
enableArchiveFileCompression="true"
archiveEvery="Sunday"
archiveAboveSize="10000000"
maxArchiveDays ="28"
archiveDateFormat="yyyy-MM-dd"
concurrentWrites="false"/>

NLog likes to have single static file, but maybe this is possible:
<target xsi:type="File"
name="error"
layout="${longdate} ${uppercase:${level}}: - ${message}"
fileName="${basedir}/logs/Error.${date:format=ddd}.log"
archiveFileName="${basedir}/logs/archive/Error.{#}.zip"
archiveDateFormat="yyyy-MM-dd"
archiveNumbering="DateAndSequence"
archiveEvery="Day"
enableArchiveFileCompression="true"
archiveAboveSize="10000000"
maxArchiveDays ="28"
concurrentWrites="false"/>
Then ${basedir}/logs/-folder will grow to have 7 files:
Error.Mon.log
Error.Tue.log
Error.Wed.log
Error.Thu.log
Error.Fri.log
Error.Sat.log
Error.Sun.log
And when "rolling" to next file that already exists (ex. Error.Mon.log), then the old file will be moved to ${basedir}/logs/archive/-folder and renamed to Error.yyyy-MM-dd.log (Using timestamp of when the old file was created)
See also: https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples

Related

NLog default wrapper usage in JSON-config

Documentation says:
Sometimes we require ALL targets to be wrapped in the same way, for example to add buffering and/or retrying. NLog provides <default-wrapper /> syntax for that. You simply put this element in the <targets /> section and all your targets will be automatically wrapped with the specified wrapper.
It also provides the following example:
<nlog>
<targets>
<default-wrapper xsi:type="BufferingWrapper" bufferSize="100"/>
<target name="f1" xsi:type="File" fileName="f1.txt"/>
<target name="f2" xsi:type="File" fileName="f2.txt"/>
</targets>
<targets>
<default-wrapper xsi:type="AsyncWrapper">
<wrapper-target xsi:type="RetryingWrapper"/>
</default-wrapper>
<target name="n1" xsi:type="Network" address="tcp://localhost:4001"/>
<target name="n2" xsi:type="Network" address="tcp://localhost:4002"/>
<target name="n3" xsi:type="Network" address="tcp://localhost:4003"/>
</targets>
</nlog>
So, <default-wrapper /> element should be a child of <targets> to be applied to each <target> within the same parent.
On the other hand, Extended Json NLog Config Example uses default-wrapper on the same level as targets element:
"default-wrapper": {
"type": "AsyncWrapper",
"overflowAction": "Block"
},
"targets": {
So, I have the following questions:
Is the Extended Json NLog Config Example correct?
Will all targets from targets element be wrapped with the default-wrapper, placed out of the targets?
What if there are default-wrapper within targets and default-wrapper out of targets in the same configuration file?
Yes there was some discussion about whether XML-config and JSON-config should be completely 1-to-1. See also: https://github.com/NLog/NLog.Extensions.Logging/pull/283
Because JSON-config represented targets-section as a dictionary of known target-names, then it didn't feel natural to have default-wrapper and default-target-parameters as reserved magic-strings.
Instead they were moved out of targets-section, as you have discovered from the documentation.
Yes all items in the targets-section will be wrapped, when using default-wrapper.
Have not tested what happens if placing default-wrapper and default-target-parameters inside the targets-section-dictionary. Maybe check the NLog InternalLogger whether it complains about unknown configuration-items.
Notice more work is being done to give default-wrapper and default-target-parameters a better name, since it might not be easy to guess that they both apply to the targets-section. See also: https://github.com/NLog/NLog.Extensions.Logging/pull/500

How can I create a NLog Config Timeshift by hours in C#?

How can I, if possible, create a log file with a timestamp that is shifted by x hours?
<variable name="logdt" value="${shortdate}"/>
<targets>
<target
name="file" xsi:type="File"
layout="${shortdate}${semi}${time}${semi}${message}"
fileName="${basedir}\logs\${logdt}**-6h**.csv"
archiveFileName="${basedir}\archives\${logdt}**-6h**.{#}.zip"
archiveEvery="Day"
archiveNumbering="Rolling"
maxArchiveFiles="7"
enableArchiveFileCompression="true"
keepFileOpen="true"
openFileCacheTimeout="30"
/>
</targets>
You can configure the Time Source.
https://github.com/NLog/NLog/wiki/Time-Source

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

Write records in descending order in NLog

I have a NLog configutation that writes to a file:
<targets>
<target name="file"
xsi:type="File"
layout="${longdate} | ${level} | ${message}"
fileName="${basedir}\logs\log.txt"
archiveFileName="${basedir}\logs\log.{#}.txt"
archiveEvery="Day"
archiveNumbering="Rolling"
maxArchiveFiles="7" />
</targets>
I want the most recent record to appear at the top of the txt file, so I don't have to scroll all the way to the bottom every time I open it since the logs can get very long. This seems like it should be in the configuration, but I'm not seeing it.
Any ideas?

Categories

Resources