Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using other log4net logging levels than the usual ones

I've realised that there is more levels than all, debug, info, warn, error and fatal, they are listed in the log4net.Core.Level class.

But how can I use them? I mean, in the ILog interface you have methods to use the usual ones, but what if you want to use "Fine" or "Emergency", etc?

Cheers.

like image 580
vtortola Avatar asked Apr 20 '10 14:04

vtortola


People also ask

What is level value in log4net?

log4net offers the following log levels, in increasing order of priority: ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF. The ALL level logs everything and the OFF level logs nothing.

Is log4net dependent on log4j?

Log4net is a logging utility for . NET applications. It's based on log4j, which is for Java applications. Log4net is highly configurable, so you can use it in many scenarios.

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.

What are log4net Appenders?

For log4net to know where to store your log messages, you add one or more appenders to your configuration. An appender is a C# class that can transform a log message, including its properties, and persist it somewhere. Examples of appenders are the console, a file, a database, an API call, elmah.io, etc.


2 Answers

In the following example Log is of type ILog.

Log.Logger.Log(null, log4net.Core.Level.Emergency, "Help!", null);

For each level you check their Value in order to know when they are disabled.

For log4net version 1.2.10.0 you have the following levels and associated cut off values:

OFF: 2147483647
EMERGENCY: 120000
FATAL: 110000
ALERT: 100000
CRITICAL: 90000
SEVERE: 80000
ERROR: 70000
WARN: 60000
NOTICE: 50000
INFO: 40000
DEBUG: 30000
FINE: 30000
TRACE: 20000
FINER: 20000
VERBOSE: 10000
FINEST: 10000
ALL: -2147483648

Note that some levels share the same values so disabling one of them will also disable the other, like for example TRACE and FINER.

like image 119
João Angelo Avatar answered Oct 18 '22 22:10

João Angelo


To extend on the good answer already provided:

You can also add the following extension method:

 static public void Notice( this ILog log, object message )
 { 
     log.Logger.Log( null, log4net.Core.Level.Notice, message, null);
 }

Then use as:

Log.Notice("Take note!");

Where Log is of type ILog

like image 38
acarlon Avatar answered Oct 18 '22 21:10

acarlon