LogManager.cs :  » Persistence-Frameworks » iBATIS.NET » IBatisNet » Common » Logging » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » iBATIS.NET 
iBATIS.NET » IBatisNet » Common » Logging » LogManager.cs

#region Apache Notice
/*****************************************************************************
 * $Header: $
 * $Revision: 474141 $
 * $Date: 2006-11-13 05:43:37 +0100 (lun., 13 nov. 2006) $
 * 
 * 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{
  /// <summary>
  /// Uses the specified <see cref="ILoggerFactoryAdapter" /> to create <see cref="ILog" /> instances
  /// that are used to log messages. Inspired by log4net.
  /// </summary>
  public sealed class LogManager
  {
    private static ILoggerFactoryAdapter _adapter = null;
    private static object _loadLock = new object();
    private static readonly string IBATIS_SECTION_LOGGING = "iBATIS/logging";

    /// <summary>
    /// Initializes a new instance of the <see cref="LogManager" /> class. 
    /// </summary>
    /// <remarks>
    /// Uses a private access modifier to prevent instantiation of this class.
    /// </remarks>
    private LogManager()
    { }


    /// <summary>
    /// Gets or sets the adapter.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The IBatisNet.Common assembly ships with the following built-in <see cref="ILoggerFactoryAdapter" /> implementations:
    /// </para>
    ///  <list type="table">
    ///  <item><term><see cref="ConsoleOutLoggerFA" /></term><description>Writes output to Console.Out</description></item>
    ///  <item><term><see cref="TraceLoggerFA" /></term><description>Writes output to the System.Diagnostics.Trace sub-system</description></item>
    ///  <item><term><see cref="NoOpLoggerFA" /></term><description>Ignores all messages</description></item>
    ///  </list>
    /// </remarks>
    /// <value>The adapter.</value>
    public static ILoggerFactoryAdapter Adapter
    {
      get
      {
        if ( _adapter == null )
        {
          lock (_loadLock)
          {
            if (_adapter == null)
            {  
              _adapter = BuildLoggerFactoryAdapter();
            }
          }
        }
        return _adapter;        
      }
      set
      {
        lock (_loadLock)
        {
          _adapter = value;
        }
      }

    }


    /// <summary>
    /// Gets the logger.
    /// </summary>
    /// <param name="type">The type.</param>
    /// <returns></returns>
    public static ILog GetLogger( Type type )
    {
      return Adapter.GetLogger( type );
    }


    /// <summary>
    /// Gets the logger.
    /// </summary>
    /// <param name="name">The name.</param>
    /// <returns></returns>
    public static ILog GetLogger( string name )
    {
      return Adapter.GetLogger(name);
    }


    /// <summary>
    /// Builds the logger factory adapter.
    /// </summary>
    /// <returns></returns>
    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;
    }


    /// <summary>
    /// Builds the default logger factory adapter.
    /// </summary>
    /// <returns></returns>
    private static ILoggerFactoryAdapter BuildDefaultLoggerFactoryAdapter()
    {
      return new NoOpLoggerFA();
    }
  }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.