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
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...
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.
In case off someone still need the answer (without using System.Reflection) It's not necessary to set DeclaringType, just set null (auto resolve in Lo4Net)
public bool IsVerboseEnable { get { return _log.Logger.IsEnabledFor(Level.Verbose); } }
public string Verbose(string text)
{
_log.Logger.Log(null, Level.Verbose, text, null);
return text;
}
Tested & Validated
Code use in log4net
public virtual void Log(Type callerStackBoundaryDeclaringType, Level level, object message, Exception exception)
{
try
{
if (this.IsEnabledFor(level))
{
this.ForcedLog((callerStackBoundaryDeclaringType != null) ? callerStackBoundaryDeclaringType : Logger.declaringType, level, message, exception);
}
}
catch (Exception exception2)
{
LogLog.Error(Logger.declaringType, "Exception while logging", exception2);
}
}
You can add a Verbose (or Trace level) to log4net by using extension methods. This is what I'm using:
public static class ILogExtentions
{
public static void Trace(this ILog log, string message, Exception exception)
{
log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType,
log4net.Core.Level.Trace, message, exception);
}
public static void Trace(this ILog log, string message)
{
log.Trace(message, null);
}
public static void Verbose(this ILog log, string message, Exception exception)
{
log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType,
log4net.Core.Level.Verbose, message, exception);
}
public static void Verbose(this ILog log, string message)
{
log.Verbose(message, null);
}
}
Usage example:
public class ClientDAO
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ClientDAO));
public void GetClientByCode()
{
log.Trace("your verbose message here");
//....
}
}
Source:
http://www.matthewlowrance.com/post/2010/07/14/Logging-to-Trace-Verbose-etc-with-log4net.aspx
I've tested log4net with BasicConfigurator, and writing log messages generated output for all levels from EMERGENCY down to DEBUG, but not for TRACE or VERBOSE.
I needed to execute the code below for them to start logging.
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
((Hierarchy)logRepository).Root.Level = Level.All;