Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure NLog to only log from a certain level for a logger namespace for *all* targets

Tags:

c#

logging

nlog

I have the following loggers configured.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="file" xsi:type="File" fileName="trace.log"/>
    <target name="trace" xsi:type="OutputDebugString"/>
    <target name="console" xsi:type="ColoredConsole" />
  </targets>
  <rules>
      <logger name="*" minlevel="Info" writeTo="file" />
      <logger name="*" minlevel="Info" writeTo="trace" />
      <logger name="*" minlevel="Info" writeTo="console" />
  </rules>
</nlog>

I want everything for Component.* to only be logged from WARN and higher for all loggers. With NHibernate, this is easy:

<logger name="NHibernate.SQL">
  <level value="OFF"/>
</logger>

I tried to add the following:

<logger name="Component.*" minlevel="Warn" final="true" />

This doesn't work.

How do I only log from a certain level for a logger namespace for all targets?

like image 601
Ramon Smits Avatar asked Apr 12 '16 15:04

Ramon Smits


People also ask

Which of the following logging level does NLog support?

NLog supports the following levels: Trace - Very detailed log messages, potentially of a high frequency and volume. Debug -Less detailed and/or less frequent debugging messages. Info - Informational messages.

What can be configured with this NLog config file?

The following types can be configured: Targets - the destinations of a logevent, e.g. file, database, console. Layout - the layout e.g. json, csv, plain-text (default) Layout renderers - the template markers, e.g. ${message}, ${exception}, ${date}

How do I store log in database using NLog?

To start logging, we need to create a Logger instance. Before creating the Logger instance, we are configuring Nlog by passing the configuration file nlog. config which we are going to create in the next section. The GetCurrentClassLogger() method returns the Logger instance with the name of the current class (Nlog.

How do I add file logging using nlog framework?

This article demonstrates how to add file logging using NLog Framework. You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or… Step 1: Create an empty .NET core web api project inside visual studio. Step 3: Add a configuration file into the project. For example, “nlog.config”

Why use a named logger per class in nlog?

We wanted to keep NLog’s default convention of using a named logger per class, which would allow us to easily query the log output for information related to a particular class. Below is a sample class showing how to get the logger, set its customer ID (used by all subsequent log methods), and log a message:

Where do you store the logs in nlog?

Target property sets where do you want to store the logs. NLog currently has 84 options for the destination of your logs: file, mail, different databases, Azure products like Azure Insights, Table Storage, or Azure Hub Event. For the implementation of your own custom targets, you must inheritance TargetWithLayout class.

What is the nlog API?

NLog provides a great API for logging messages to one or more targets. If you are already familiar with similar logging frameworks, you will quickly be up and running logging with NLog. Let's dig into the details. All log messages have a severity called "Level". The following log levels are supported:


Video Answer


1 Answers

The solution is:

<logger name="Component.*" maxlevel="Info" final="true" />

You basically say, for logger(s) X, I want to skip all log entries that match Info or lower as this does not have the writeTo attribute.

It is documented here:

https://github.com/nlog/NLog/wiki/Configuration-file

With the sample:

<logger name="Name.Space.*" minlevel="Debug" maxlevel="Error" final="true" />
like image 83
Ramon Smits Avatar answered Sep 19 '22 00:09

Ramon Smits