Author: rgrabowski
Date: Wed Sep 21 20:00:10 2005
New Revision: 290879
URL: http://svn.apache.org/viewcvs?rev=290879&view=rev
Log:
Log4net is capable of outputting extended debug information about where the
current message was generated: class name, method name, file, line, etc.
Log4net assumes that the location information should be gathered relative to
where log4net.ILog.Debug() was called. In IBatisNet, log4net.ILog.Debug() is
called in IBatisNet.Common.Logging.Impl.Log4NetLogger. This means that the
location information will indicate that
IBatisNet.Common.Logging.Impl.Log4NetLogger always made the call to
log4net.ILog.Debug(). We need to know where
IBatisNet.Common.Logging.ILog.Debug() was called. To do this we need to use the
log4net.ILog.Logger.Log method and pass in a Type telling log4net where in the
stack to begin looking for location information.
Added:
ibatis/trunk/cs/mapper/IBatisNet.Common.Logging.Log4Net/LocationInfoLog4NetLogger.cs
ibatis/trunk/cs/mapper/IBatisNet.Common.Logging.Log4Net/LocationInfoLog4NetLoggerFA.cs
Added:
ibatis/trunk/cs/mapper/IBatisNet.Common.Logging.Log4Net/LocationInfoLog4NetLogger.cs
URL:
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common.Logging.Log4Net/LocationInfoLog4NetLogger.cs?rev=290879&view=auto
==============================================================================
---
ibatis/trunk/cs/mapper/IBatisNet.Common.Logging.Log4Net/LocationInfoLog4NetLogger.cs
(added)
+++
ibatis/trunk/cs/mapper/IBatisNet.Common.Logging.Log4Net/LocationInfoLog4NetLogger.cs
Wed Sep 21 20:00:10 2005
@@ -0,0 +1,272 @@
+#region Apache Notice
+/*****************************************************************************
+ * $Header: $
+ * $Revision: $
+ * $Date: $
+ *
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2004 - Gilles Bayon
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+
********************************************************************************/
+#endregion
+
+#region Using
+using System;
+using IBatisNet.Common.Logging;
+using log4net.Core;
+
+#endregion
+
+namespace IBatisNet.Common.Logging.Impl
+{
+ /// <summary>
+ /// Specialized Log4NetLogger that correctly preserves location
information.
+ /// </summary>
+ /// <remarks>
+ /// Log4net is capable of outputting extended debug information about
where the current
+ /// message was generated: class name, method name, file, line, etc.
Log4net assumes that the location
+ /// information should be gathered relative to where
log4net.ILog.Debug() was called. In IBatisNet,
+ /// log4net.ILog.Debug() is called in
IBatisNet.Common.Logging.Impl.Log4NetLogger. This means that
+ /// the location information will indicate that
IBatisNet.Common.Logging.Impl.Log4NetLogger always made
+ /// the call to log4net.ILog.Debug(). We need to know where
IBatisNet.Common.Logging.ILog.Debug()
+ /// was called. To do this we need to use the log4net.ILog.Logger.Log
method and pass in a Type telling
+ /// log4net where in the stack to begin looking for location
information.
+ /// </remarks>
+ public class LocationInfoLog4NetLogger : ILog
+ {
+ #region Fields
+
+ private log4net.ILog _log = null;
+ private readonly static Type declaringType =
typeof(LocationInfoLog4NetLogger);
+
+ #endregion
+
+ /// <summary>
+ /// Constructor
+ /// </summary>
+ /// <param name="log"></param>
+ internal LocationInfoLog4NetLogger(log4net.ILog log )
+ {
+ _log = log;
+ }
+
+ #region ILog Members
+
+ /// <summary>
+ ///
+ /// </summary>
+ public bool IsInfoEnabled
+ {
+ get { return _log.IsInfoEnabled; }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ public bool IsWarnEnabled
+ {
+ get { return _log.IsWarnEnabled; }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ public bool IsErrorEnabled
+ {
+ get { return _log.IsErrorEnabled; }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ public bool IsFatalEnabled
+ {
+ get { return _log.IsFatalEnabled; }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ public bool IsDebugEnabled
+ {
+ get { return _log.IsDebugEnabled; }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ public bool IsTraceEnabled
+ {
+ get { return _log.IsDebugEnabled; }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ /// <param name="e"></param>
+ public void Info(object message, Exception e)
+ {
+ if (_log.IsInfoEnabled)
+ {
+ _log.Logger.Log(declaringType, Level.Info,
message, e);
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ public void Info(object message)
+ {
+ if (_log.IsInfoEnabled)
+ {
+ _log.Logger.Log(declaringType, Level.Info,
message, null);
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ /// <param name="e"></param>
+ public void Debug(object message, Exception e)
+ {
+ if (_log.IsDebugEnabled)
+ {
+ _log.Logger.Log(declaringType, Level.Debug,
message, e);
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ public void Debug(object message)
+ {
+ if (_log.IsDebugEnabled)
+ {
+ _log.Logger.Log(declaringType, Level.Debug,
message, null);
+ }
+ }
+
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ /// <param name="e"></param>
+ public void Warn(object message, Exception e)
+ {
+ if (_log.IsWarnEnabled)
+ {
+ _log.Logger.Log(declaringType, Level.Warn,
message, e);
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ public void Warn(object message)
+ {
+ if (_log.IsWarnEnabled)
+ {
+ _log.Logger.Log(declaringType, Level.Warn,
message, null);
+ }
+ }
+
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ /// <param name="e"></param>
+ public void Trace(object message, Exception e)
+ {
+ // is calling through to _log.Logger slow ???
+ if (_log.Logger.IsEnabledFor(Level.Trace))
+ {
+ _log.Logger.Log(declaringType, Level.Trace,
message, e);
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ public void Trace(object message)
+ {
+ // is calling through to _log.Logger slow ???
+ if (_log.Logger.IsEnabledFor(Level.Trace))
+ {
+ _log.Logger.Log(declaringType, Level.Info,
message, null);
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ /// <param name="e"></param>
+ public void Fatal(object message, Exception e)
+ {
+ if (_log.IsFatalEnabled)
+ {
+ _log.Logger.Log(declaringType, Level.Fatal,
message, e);
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ public void Fatal(object message)
+ {
+ if (_log.IsFatalEnabled)
+ {
+ _log.Logger.Log(declaringType, Level.Fatal,
message, null);
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ /// <param name="e"></param>
+ public void Error(object message, Exception e)
+ {
+ if (_log.IsErrorEnabled)
+ {
+ _log.Logger.Log(declaringType, Level.Error,
message, e);
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ public void Error(object message)
+ {
+ if (_log.IsErrorEnabled)
+ {
+ _log.Logger.Log(declaringType, Level.Error,
message, null);
+ }
+ }
+
+ #endregion
+ }
+}
\ No newline at end of file
Added:
ibatis/trunk/cs/mapper/IBatisNet.Common.Logging.Log4Net/LocationInfoLog4NetLoggerFA.cs
URL:
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common.Logging.Log4Net/LocationInfoLog4NetLoggerFA.cs?rev=290879&view=auto
==============================================================================
---
ibatis/trunk/cs/mapper/IBatisNet.Common.Logging.Log4Net/LocationInfoLog4NetLoggerFA.cs
(added)
+++
ibatis/trunk/cs/mapper/IBatisNet.Common.Logging.Log4Net/LocationInfoLog4NetLoggerFA.cs
Wed Sep 21 20:00:10 2005
@@ -0,0 +1,117 @@
+#region Apache Notice
+/*****************************************************************************
+ * $Header: $
+ * $Revision: $
+ * $Date: $
+ *
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2004 - Gilles Bayon
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+
********************************************************************************/
+#endregion
+
+#region Using
+
+using System;
+using System.Collections.Specialized;
+using System.Configuration;
+using System.IO;
+using log4net.Config;
+
+using IBatisNet.Common.Logging;
+#endregion
+
+namespace IBatisNet.Common.Logging.Impl
+{
+ /// <summary>
+ /// Concrete subclass of ILoggerFactoryAdapter specific to log4net that
preserves location information.
+ /// This class is intended primarily for IBatisNet developers.
+ /// </summary>
+ public class LocationInfoLog4NetLoggerFA : ILoggerFactoryAdapter
+ {
+ /// <summary>
+ /// Constructor
+ /// </summary>
+ /// <param name="properties"></param>
+ public LocationInfoLog4NetLoggerFA(NameValueCollection
properties)
+ {
+ string configurationType = string.Empty;
+
+ if ( properties["configType"] != null )
+ {
+ configurationType =
properties["configType"].ToUpper();
+ }
+
+ string configurationFile = string.Empty;
+ if ( properties["configFile"] != null )
+ {
+ configurationFile = properties["configFile"];
+ }
+
+ if ( configurationType == "FILE" || configurationType
== "FILE-WATCH" )
+ {
+ if ( configurationFile == string.Empty )
+ throw new ConfigurationException(
"Configration property 'configurationFile' must be set for log4Net
configuration of type 'FILE'." );
+
+ if ( !File.Exists( configurationFile ) )
+ throw new ConfigurationException(
"log4net configuration file '" + configurationFile + "' does not exists" );
+ }
+
+ switch ( configurationType )
+ {
+ case "INLINE":
+ XmlConfigurator.Configure();
+ break;
+ case "FILE":
+ XmlConfigurator.Configure( new
FileInfo( configurationFile ) );
+ break;
+ case "FILE-WATCH":
+ XmlConfigurator.ConfigureAndWatch( new
FileInfo( configurationFile ) );
+ break;
+ case "EXTERNAL":
+ // Log4net will be configured outside
of IBatisNet
+ break;
+ default:
+ BasicConfigurator.Configure();
+ break;
+ }
+ }
+
+ #region ILoggerFactoryAdapter Members
+
+ /// <summary>
+ /// Get a ILog instance by type name
+ /// </summary>
+ /// <param name="name"></param>
+ /// <returns></returns>
+ public ILog GetLogger(string name)
+ {
+ return new
LocationInfoLog4NetLogger(log4net.LogManager.GetLogger(name));
+ }
+
+ /// <summary>
+ /// Get a ILog instance by type
+ /// </summary>
+ /// <param name="type"></param>
+ /// <returns></returns>
+ public ILog GetLogger(Type type)
+ {
+ return new
LocationInfoLog4NetLogger(log4net.LogManager.GetLogger(type));
+ }
+
+ #endregion
+ }
+}