CapturingLoggerFactoryAdapter.cs :  » Logging-Tools » Common.Logging » Common » Logging » Simple » 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 » Logging Tools » Common.Logging 
Common.Logging » Common » Logging » Simple » CapturingLoggerFactoryAdapter.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace Common.Logging.Simple{
    /// <summary>
    /// An adapter, who's loggers capture all log events and send them to <see cref="AddEvent"/>. 
    /// Retrieve the list of log events from <see cref="LoggerEvents"/>.
    /// </summary>
    /// <remarks>
    /// This logger factory is mainly for debugging and test purposes.
    /// <example>
    /// This is an example how you might use this adapter for testing:
    /// <code>
    /// // configure for capturing
    /// CapturingLoggerFactoryAdapter adapter = new CapturingLoggerFactoryAdapter();
    /// LogManager.Adapter = adapter;
    /// 
    /// // reset capture state
    /// adapter.Clear();
    /// // log something
    /// ILog log = LogManager.GetCurrentClassLogger();
    /// log.DebugFormat(&quot;Current Time:{0}&quot;, DateTime.Now);
    /// 
    /// // check logged data
    /// Assert.AreEqual(1, adapter.LoggerEvents.Count);
    /// Assert.AreEqual(LogLevel.Debug, adapter.LastEvent.Level);
    /// </code>
    /// </example>
    /// </remarks>
    /// <author>Erich Eichinger</author>
    public class CapturingLoggerFactoryAdapter : ILoggerFactoryAdapter
    {
        private readonly Hashtable _cachedLoggers = CollectionsUtil.CreateCaseInsensitiveHashtable();

        private volatile CapturingLoggerEvent _lastEvent;

        /// <summary>
        /// Holds the last log event received from any of this adapter's loggers.
        /// </summary>
        public CapturingLoggerEvent LastEvent
        {
            get { return _lastEvent; }
        }

        /// <summary>
        /// Clears all captured events
        /// </summary>
        public void Clear()
        {
            lock(LoggerEvents)
            {
                ClearLastEvent();
                LoggerEvents.Clear();
            }
        }

        /// <summary>
        /// Resets the <see cref="LastEvent"/> to <c>null</c>.
        /// </summary>
        public void ClearLastEvent()
        {
            _lastEvent = null;
        }

        /// <summary>
        /// Holds the list of logged events.
        /// </summary>
        /// <remarks>
        /// To access this collection in a multithreaded application, put a lock on the list instance.
        /// </remarks>
        public readonly IList<CapturingLoggerEvent> LoggerEvents = new List<CapturingLoggerEvent>();

        /// <summary>
        /// <see cref="CapturingLogger"/> instances send their captured log events to this method.
        /// </summary>
        public virtual void AddEvent(CapturingLoggerEvent loggerEvent)
        {
            _lastEvent = loggerEvent;
            lock (LoggerEvents)
            {
                LoggerEvents.Add(loggerEvent);
            }
        }

        /// <summary>
        /// Get a <see cref="CapturingLogger"/> instance for the given type.
        /// </summary>
        public ILog GetLogger(Type type)
        {
            return GetLogger(type.FullName);
        }

        /// <summary>
        /// Get a <see cref="CapturingLogger"/> instance for the given name.
        /// </summary>
        public ILog GetLogger(string name)
        {
            ILog logger = (ILog)_cachedLoggers[name];
            if (logger == null)
            {
                lock (_cachedLoggers.SyncRoot)
                {
                    logger = (ILog)_cachedLoggers[name];
                    if (logger == null)
                    {
                        logger = new CapturingLogger(this, name);
                        _cachedLoggers[name] = logger;
                    }
                }
            }
            return logger;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.