#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
using System;
using System.Configuration;
using IBatisNet.Common.Logging.Impl;
namespace IBatisNet.Common.Logging
{
///
/// Uses the specified to create instances
/// that are used to log messages. Inspired by log4net.
///
public sealed class LogManager
{
private static ILoggerFactoryAdapter _adapter = null;
private static object _loadLock = new object();
private static readonly string IBATIS_SECTION_LOGGING = "iBATIS/logging";
///
/// Initializes a new instance of the class.
///
///
/// Uses a private access modifier to prevent instantiation of this class.
///
private LogManager()
{ }
///
/// Gets or sets the adapter.
///
///
///
/// The IBatisNet.Common assembly ships with the following built-in implementations:
///
///
/// - Writes output to Console.Out
/// - Writes output to the System.Diagnostics.Trace sub-system
/// - Ignores all messages
///
///
/// The adapter.
public static ILoggerFactoryAdapter Adapter
{
get
{
if ( _adapter == null )
{
lock (_loadLock)
{
if (_adapter == null)
{
_adapter = BuildLoggerFactoryAdapter();
}
}
}
return _adapter;
}
set
{
lock (_loadLock)
{
_adapter = value;
}
}
}
///
/// Gets the logger.
///
/// The type.
///
public static ILog GetLogger( Type type )
{
return Adapter.GetLogger( type );
}
///
/// Gets the logger.
///
/// The name.
///
public static ILog GetLogger( string name )
{
return Adapter.GetLogger(name);
}
///
/// Builds the logger factory adapter.
///
///
private static ILoggerFactoryAdapter BuildLoggerFactoryAdapter()
{
LogSetting setting = null;
try
{
#if dotnet2
setting = (LogSetting)ConfigurationManager.GetSection(IBATIS_SECTION_LOGGING );
#else
setting = (LogSetting)ConfigurationSettings.GetConfig( IBATIS_SECTION_LOGGING );
#endif
}
catch ( Exception ex )
{
ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
ILog log = defaultFactory.GetLogger( typeof(LogManager) );
log.Warn( "Unable to read configuration. Using default logger.", ex );
return defaultFactory;
}
if ( setting!= null && !typeof ( ILoggerFactoryAdapter ).IsAssignableFrom( setting.FactoryAdapterType ) )
{
ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
ILog log = defaultFactory.GetLogger( typeof(LogManager) );
log.Warn( "Type " + setting.FactoryAdapterType.FullName + " does not implement ILoggerFactoryAdapter. Using default logger" );
return defaultFactory;
}
ILoggerFactoryAdapter instance = null;
if (setting!=null)
{
if (setting.Properties.Count>0)
{
try
{
object[] args = {setting.Properties};
instance = (ILoggerFactoryAdapter)Activator.CreateInstance( setting.FactoryAdapterType, args );
}
catch ( Exception ex )
{
ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
ILog log = defaultFactory.GetLogger( typeof(LogManager) );
log.Warn( "Unable to create instance of type " + setting.FactoryAdapterType.FullName + ". Using default logger.", ex );
return defaultFactory;
}
}
else
{
try
{
instance = (ILoggerFactoryAdapter)Activator.CreateInstance( setting.FactoryAdapterType );
}
catch ( Exception ex )
{
ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
ILog log = defaultFactory.GetLogger( typeof(LogManager) );
log.Warn( "Unable to create instance of type " + setting.FactoryAdapterType.FullName + ". Using default logger.", ex );
return defaultFactory;
}
}
}
else
{
ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
ILog log = defaultFactory.GetLogger( typeof(LogManager) );
log.Warn( "Unable to read configuration IBatisNet/logging. Using default logger." );
return defaultFactory;
}
return instance;
}
///
/// Builds the default logger factory adapter.
///
///
private static ILoggerFactoryAdapter BuildDefaultLoggerFactoryAdapter()
{
return new NoOpLoggerFA();
}
}
}