in log4net, is it possible to mute loggers by name rather than by level only? - log4net

I am building a new C# project using the DLLs produced by some colleagues.
They have log4net statements at all levels popping around in such number that my logs are hardly visible in the mass...
in log4net, is it possible to mute loggers by name rather than by level only ?
or even better, by assembly name ? I want to mute logging in DLLs that I use...
Thank you guys for any hints !

It should be a matter of adding a section to your config file like
<logger name="xxx" additivity="false">
<level value="OFF" />
</logger>
If logging is using the standard convention, ie
internal static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
then you need to replace xxx can be the namespace or part of the namespace that you want to disable.
If your colleagues are using the exact same namespace name as you are using,then you might need to replace xxx with the actual class name which could mean you end up with a large number of entries in your config file to disable logging for each of their classes.

Related

NLog filename per thread

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.

How to use NLog to write to different file according to parameter

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

Is there any logging function which allows you to create n number of log files programatically?

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

how to switch log4net multiple log files using C# code?

I'm using log4net, I would like to have 2 logs,
- BasketballCustomer.log, for all Customers that plays Basketball;
- ChessCustomer.log, for all Customers that plays Chess.
, while for each customer, whether he/she plays Basketball or Chess, is only known until runtime.
I would like to have each log configured separately, about log file name, size, number, log level, etc.
Also, I'd prefer such set up done by C# code, not config file.
How could I do that?
I tried search on net, there are some articles but none meet exactly my requirements
- Log4Net and multiple log files talked about multiple log files but it does not toggle during runtime;
- Configure Log4net to write to multiple files is similiar but it's done in config file....
Please kindly suggest, many thanks!
You can do this by using an environment variable in the log4net.config and then set the value of the environment variable through the C# code
So somewhere in your C# class, do something like:
Environment.SetEnvironmentVariable("log_file_name", "MyLogFileName");
And then in the log4net.config that is used, specify the value to the name of the environment variable. The syntax would be something like this:
<param name="File" value="${log_file_name}".log/>

How do I create a new file for each log4net logger instance?

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" />

Resources