I'm trying to create log filename dynamically using NLog (.NET Framework 4.8). Basically, I would like unique filename created every time new thread is fired. I got it somewhat working with the following sections:
<target xsi:type="File" name="ownFile" header="${machinename}${newline}${threadid}${newline}"
fileName="${mdc:logFolder}\${date:format=yyyyMMdd}\log_${activityid}.txt" layout="${date}|${activityid}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}"
footer="${newline}Finished at: ${date}"/>
<logger name="*" minlevel="Debug" writeTo="ownFile" />
When thread is started, it sets the logFolder using MappedDiagnosticsContext.Set and sets the Trace.CorrelationManager.ActivityId. But there is one problem. The footer part is only written when FileTarget is disposed, which I believe then closes all file appenders cached inside FileTarget.
Is there a way for me to manually close the internal file appender for dynamic file? What I want is when thread is done with its processing is to close the dynamic log file.
I also have another approach working where I add new FileTarget when thread starts and the remove it when thread is finished. It does work, but I would prefer config file solution.
Thanks in advance.
Related
I am using NLog.
I would like to have a size-based file archival and a time-based file archival.
Meaning, every time the log file exceeds 10 MB a new log file is created. Also, every day a new log file is created.
It is clear how to do each of the above separately (https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples) but not how to use them in tandem.
Without any details of the expected fileName-Layout, then this will work just fine in NLog 4.5 (and newer):
<target type="file" name="logfile" fileName="App-${shortdate}.log" archiveAboveSize="1000000" maxArchiveFiles="30" />
It will produce the following filenames (newest first)
App-20200216.log
App-20200216.2.log
App-20200216.1.log
App-20200215.log
App-20200214.log
App-20200214.1.log
See also: https://github.com/NLog/NLog/wiki/File-target#archive-old-log-files
Is there any event handler or similar that I can hook into, to run some code every time NLog starts a new Log File?
I have an NLog Config like this:
<target name="logfile" xsi:type="File"
fileName="[stuff]"
layout="[more Stuff]"
maxArchiveFiles="4"
archiveAboveSize="102400" />
And I'd kinda like to add some meta-data at the start of every log file (version number, Machine Name, etc. so that every log file comes complete with some contextual information)
I don't expect to be able to configure this text in NLog itself, but I'm hoping that I can tell NLog to run a certain method everytime it's going to create a new Log File.
Is this a thing?
Sounds like you are really looking for the Header-Layout on the FileTarget.
<target name="logfile" xsi:type="File">
<header>--- ${assembly-version} started on ${longdate} ---</header>
</target>
See also https://github.com/NLog/NLog/wiki/File-target
Some combine it with archiveOldFileOnStartup="true" because the Header is only printed when file is created (ensure application restarts will create new file).
You can use FileSystemWatcher.
FileSystemWatcher is watching specific (which you want it) directory, If new file created, handle this event with FileSystemWatcher
For more details : https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=netframework-4.8
Another partial solution to the "What version?" aspect of this problem could come from:
https://github.com/NLog/NLog/wiki/AssemblyVersion-Layout-Renderer
I want to be able to write to logs to a different log file according to one of my log parameters.
Lets say, bankId. Each day and bank will write to their own file like
2019-11-11-bank11.log
2019-11-12-bank11.log
and so on.
How can i achieve that programmatic with some kind of a pattern for the log file.
I don't want to create a new version of my app every time I have a new bankid.
You could set the context in one of the context classes and render it with a layout renderer.
e.g. use ${mdlc} and MappedDiagnosticsLogicalContext:
Config:
<target name="file" xsi:type="File"
fileName="${basedir}/${mdlc:bankid}.log" .. />
Code:
using (NLog.MappedDiagnosticsLogicalContext.SetScoped("bankid", "bank11"))
{
logger.Info("myLogEvent");
}
See also all context layout renderers
Sounds like you have an application-wide setting. For that you should either use:
NLog Config Variables - ${var:bankid}
Global Diagnostic Context - ${gdc:bankid}
I prefer to use the GDC has it has a minimum number of surprises, when modifying at runtime.
You can do this in NLog.config:
<target name="file" xsi:type="File"
fileName="${basedir}/${shortdate}-bank${gdc:bankid:whenEmpty=0}.log" .. />
Then at application startup then you can do this:
NLog.GlobalDiagnosticsContext.Set("bankid","11");
See also: https://github.com/nlog/nlog/wiki/Gdc-Layout-Renderer
You can also extract a setting from config-file:
app.config - https://github.com/NLog/NLog/wiki/AppSetting-Layout-Renderer
appsettings.json - https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer
I'm working on a program which needs to write messages to multiple log files, depending on the number of threads it creates. The amount of threads is arbitrary. Is there a logging facility which allows me to create a separate log file for each thread? I'm currently using sl4j and logback, and it seems to require that the file names be configured in a properties file.
Logback's SiftingAppender separates (or sifts) logging according to a given runtime attributes. The general structure goes likes this:
<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>clientPort</key>
<defaultValue>unknown</defaultValue>
</discriminator>
<sift>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>server-${clientPort}.log</file>
....
</appender>
</sift>
</appender>
By default, SiftingAppender assumes MDCBasedDiscriminator which relies heavily on MDC.
It is probably exactly what your are looking for. See also http://www.nurkiewicz.com/2013/04/siftingappender-logging-different.html
I am trying to create a different log file for each thread (the threads are handling some site processing). Essentially just naming the log file with a name that is derived from the site that is being processed.
I know all about the GlobalContext.properties and also the TheadContext.properties and neither really seem to work for what I am trying to do. In pretty much either case any concurrently running threads just use whichever file is the current output file as their output. The only difference I have seen is that if I use the ThreadContext to set the property it makes all the files but only outputs to one, where using global it seems to only make the one file unless the processes start at different times.
What I would REALLY like to do is be able to tell the file to just use the name of the logger object (the name that is used in instantiating the object) in the file name instead of using these properties.
Instead of fighting the configuration, I recommend that you rethink your logging strategy. log4net was not built for a log file per thread/class. Loggers are created at startup or on first write (depends). Use a tailing log file reader, like Kiwi Log Viewer or Splunk, and filter on the thread id or logger name in your messages.
If you put them in your conversion pattern...
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline" />