Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to wildcard logger names in log4net configuration?

Tags:

c#

.net

log4net

In my application, I use log4net, with all types creating their own logger based on their type - e.g. :

private static readonly ILog Log = LogManager.GetLogger(typeof(Program)); 

As I am developing, I leave the root logger on DEBUG so as to catch all log output from my code.

However, a third party component also uses this same approach, but is generating 100s of log messages a second, none of which I am interested in.

Is it possible to use some sort of wildcarding in the logger configuration, to force all their loggers to only log at WARN, e.g. :

 <logger name="com.thirdparty.*">     <level value="WARN"/>   </logger> 

[The exact example above, using a * doesn't work]

like image 979
Rob Levine Avatar asked Jan 07 '10 22:01

Rob Levine


People also ask

What are the main components of log4net briefly describe how loggers work in log4net?

Log4net has three main components: loggers, appenders and layouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

Which class gives us the object reference needed to log messages using log4net?

Using log4net. In your class, create a reference to ILog by making a call to the GetLogger static method of the LogManager class as shown in the code snippet given below.

What is log4net config?

The log4net configuration can be configured using assembly-level attributes rather than specified programmatically. If specified, this is the filename of the configuration file to use with the XmlConfigurator. This file path is relative to the application base directory (AppDomain. CurrentDomain.

How do I add a console to log4net?

After installing this package, open up AssemblyInfo. cs file under the Properties folder and add the log4net assembly information into it (under the other assembly information.). Now, open the App. config file and enter required details for LogNet to work.


1 Answers

You can just specify part of a namespace so it will apply to all messages within that namespace (including nested).

Here is the example I often use:

  <root>     <level value="FATAL" />     <appender-ref ref="RollingFile" />   </root>    <logger name="MyCompany.Web" >     <level value="WARN" />     <appender-ref ref="WebErrors" />   </logger>    <!-- Will log all FATALs from NHibernate, including NHibernate.SQL and all the nested -->   <logger name="NHibernate" >     <level value="FATAL" />   </logger> 

Additionally I would recommend to read the manual. It provides a lot of explanation. For example you can read about Logger Hierarchy. Here is the quote from there:

A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger. The hierarchy works very much in the same way as the namespace and class hierarchy in .NET.

and also:

Level Inheritance: The inherited level for a given logger X, is equal to the first non-null level in the logger hierarchy, starting at X and proceeding upwards in the hierarchy towards the root logger.

like image 169
Dmytrii Nagirniak Avatar answered Sep 20 '22 15:09

Dmytrii Nagirniak