Now that we have our own LogManager, we could borrow NLog's idea of
GetCurrentClassLogger() to return the logger for the current class
instead of the usual System.Reflection... way:
// current way
ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
);
// less verbose
ILog log = LogManager.GetCurrentClassLogger();
I haven't looked at NLog's implementation of GetCurrentClassLogger()
but I would imagine it involves digging through the stack:
StackTrace callStack = new StackTrace();
StackFrame frame = callStack.GetFrame(1); // 2 ???
MethodBase method = frame.GetMethod();
Type declaringType = method.DeclaringType; // ???
return LogManager.GetLogger(declaringType);
One could argue that doing that is "slower". A counter-argument would
be that the System.Reflection... call is slow compared to:
ILog log = LogManager.GetLogger("IBatisNet.DataAccess.DaoManager");
Its a really a non-issue since the logger is static and the call only
happens once.
Could NoOpLogger (and/or all the built-in log implementations) be
marked as sealed? I can't think of a case where it would be extended.
I'm in favor of making our ILog match log4net's ILog:
http://tinyurl.com/ch9b7
http://cvs.apache.org/viewcvs.cgi/logging-log4net/src/ILog.cs?rev=1.10&view=markup
The new beta has overloads similiar to the StringBuilder's AppendFormat
method:
DebugFormat(string format, params object[] args)
It makes things slightly cleaner:
_logger.Debug("Open Connection [{0}] to [{1}].",
_connection.GetHashCode(), // 0
_dataSource.Provider.Description)); // 1
Perhaps the SimpleLogger could have an additional option indicating
whether it should write to the Console.Out, System.Diagnostic.Trace, or
both:
// Print to the appropriate destination
System.Console.Error.WriteLine( sb.ToString() );
System.Diagnostics.Trace.WriteLine( sb.ToString() );
I believe NLog and log4net both write to Console.Out rather than
Console.Error.
If there were support for writing to Trace, one could easily redirect
the output using a TraceListener in their App.config file:
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\\IBatisNetLog.txt" />
</listeners>
</trace>
</system.diagnostics>
I wonder if these two lines are the same. I forgot that C# had a
PadRight :-)
sb.Append(string.Format("[{0}]",level.ToString().ToUpper()).PadRight(8));
sb.AppendFormat("[{0,-8}],level.ToString().ToUpper());
- Ron