Is there a way to create a named logger with NLog and log filtering using that name?
Sometimes I don't want to log everything, especially in the Visual Sutdio Output window (target -> debugger) during development. I thought maybe there is a way to name a specific logger (one class) or multiple loggers (from multiple classes) so that in the config file I can enable logging only for the classes I'm interested in at the moment.
I currently have this most common NLog line across all my classes:
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
and a very standard configuration that just prints the logs to the output window.
Unfortunately I have no luck on how to enable / disable loggers by class.
source to share
As I mentioned in the comments, you can use NLog Conditions to evaluate and determine what you want to do with Results. As the documentation says:
Conditions are filter expressions used with the when filter. They consist of one or more tests. They are> used in filters to determine if an action will be taken.
There is also a very useful example:
<rules>
<logger name="*" writeTo="file">
<filters>
<when condition="length('${message}') > 100" action="Ignore" />
<when condition="equals('${logger}','MyApps.SomeClass')" action="Ignore" />
<when condition="(level >= LogLevel.Debug and contains('${message}','PleaseDontLogThis')) or level==LogLevel.Warn" action="Ignore" />
<when condition="not starts-with('${message}','PleaseLogThis')" action="Ignore" />
</filters>
</logger>
</rules>
source to share