Log4net, how to log a verbose message?

Viewed 15120

I can log info messages without a problem, but can't figure out how to log verbose messages. Any help would be welcomed.

My problem is:

loggingEvent.Level can be checked in the Format function. The possible values are amongst others, Info, Debug, Error, Verbose. There are more, but these are the ones I'll be using mostly.

The actual log object only has the following methods:

Log.Info
Log.Debug
Log.Warn
Log.Error

As you can see - no verbose!

So how can I Log a verbose message, this is different to debug

Thanks in advance

6 Answers

Apache log4net has the following log levels:

DEBUG < INFO < WARN < ERROR < FATAL

For messages considered more verbose than informational messages (INFO), the DEBUG level is the option to go for. Writing debug messages should be as simple as:

myLog.Debug("This is a pretty verbose message");

If you write extremely many debug messages and/or the messages are costly to produce (eg involves heavy string concatenation), consider adding a conditional around the logging:

if (myLog.IsDebugEnabled)
{
    myLog.Debug("This is a pretty verbose message");
}

If you find yourself doing this often and want to DRY up your code, consider using extension methods for deferred message formatting, which will turn the above statement into this:

Log.Debug( () => "This is a pretty verbose message" );  

You cannot figure out, because, AFAIK there is no "verbose" level in log4net. Is there one in log4j?

Following are the levels

ALL DEBUG INFO WARN ERROR FATAL OFF

Informational messages are the ones where you specify what you are doing currently in your application. Those messages spit out by OS commands or tools when you say -verbose, would be these kind of messages.

Debug messages are mostly for programmers and they allow you to write information such as variable creation, life-cycle, exception stack traces etc. Something that only the programmer/ support staff would be interested in.

[Edit] Just thought of this. You can very well add a switch or config element to your application named "verbose" and then spit out the informational messages if set to true. Or wrap the logging in a helper method, which will log in log4net as well as send the same message to console. Also, you can use the ConsoleAppender to log messages to console. I have never used it though. Is this what you were looking for?

Hope this helps.

I did not try it, but I think it should be quite straight-forward: Internally log4net knows a level "verbose"; it is only the ILog interface that does not expose it. Therefore it should be quite simple to add a IsVerboseEnabled and Verbose() method to this interface. Of course you need to be willing to change the log4net source code...

Related