EntLibLoggerFactoryAdapterTests.cs :  » Logging-Tools » Common.Logging » Common » Logging » EntLib » 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 » EntLib » EntLibLoggerFactoryAdapterTests.cs
using System;
using System.Collections.Specialized;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
using NUnit.Framework;

namespace Common.Logging.EntLib{
    [TestFixture]
    public class EntLibLoggerFactoryAdapterTests
    {
        [Test]
        public void InitDefaults()
        {
            EntLibLoggerFactoryAdapter a = new EntLibLoggerFactoryAdapter();
            Assert.AreEqual(EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT, a.ExceptionFormat);
            Assert.AreEqual(EntLibLoggerSettings.DEFAULTPRIORITY, a.DefaultPriority);

            a = new EntLibLoggerFactoryAdapter(null);
            Assert.AreEqual(EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT, a.ExceptionFormat);
            Assert.AreEqual(EntLibLoggerSettings.DEFAULTPRIORITY, a.DefaultPriority);
        }

        [Test]
        public void InitWithProperties()
        {
            NameValueCollection props = new NameValueCollection();
            props["exceptionFormat"] = "$(exception.message)";
            props["priority"] = "10";
             EntLibLoggerFactoryAdapter a = new EntLibLoggerFactoryAdapter(props);
            Assert.AreEqual("$(exception.message)", a.ExceptionFormat);
            Assert.AreEqual(10, a.DefaultPriority);
        }

        [Test]
        public void CachesLoggers()
        {
            SeverityFilter severityFilter = new SeverityFilter(null, TraceEventType.Critical | TraceEventType.Error);
            TestEntLibLoggerFactoryAdapter a = CreateTestEntLibLoggerFactoryAdapter(severityFilter);

            ILog log = a.GetLogger(this.GetType());
            Assert.AreSame( log, a.GetLogger(this.GetType()) );            
        }

        [Test]
        public void LogsMessage()
        {
            SeverityFilter severityFilter = new SeverityFilter(null, TraceEventType.Critical|TraceEventType.Error);            
            TestEntLibLoggerFactoryAdapter a = CreateTestEntLibLoggerFactoryAdapter(severityFilter);
            Exception ex = new Exception("errormessage");

            ILog log = a.GetLogger(this.GetType());

            // not logged due to severity filter 
            a.LastLogEntry = null;
            log.Trace("Message1", ex);
            Assert.IsNull(a.LastLogEntry);

            // logged, passes severity filter
            a.LastLogEntry = null;
            log.Error("Message2", ex);
            Assert.AreEqual( TraceEventType.Error, a.LastLogEntry.Severity );
            Assert.AreEqual( "Message2", a.LastLogEntry.Message);
            Assert.AreEqual( a.DefaultPriority, a.LastLogEntry.Priority);
            Assert.AreEqual( 1, a.LastLogEntry.Categories.Count);
            Assert.AreEqual( this.GetType().FullName, a.LastLogEntry.CategoriesStrings[0]);
            Assert.AreEqual("Exception[ message = errormessage, source = , targetsite = , stacktrace =  ]", a.LastLogEntry.ErrorMessages.Trim());
        }

        #region TestEntLibLoggerFactoryAdapter

        private static TestEntLibLoggerFactoryAdapter CreateTestEntLibLoggerFactoryAdapter(ILogFilter filter)
        {
            LogWriter logWriter = new LogWriter(
                new ILogFilter[] { filter }
                , new LogSource[] { new LogSource("logSource") }
                , new LogSource("defaultLogSource")
                , new LogSource("notProcessedLogSource")
                , new LogSource("errorsLogSource")
                , "DefaultCategory"
                , true
                , true
                );

            return new TestEntLibLoggerFactoryAdapter(5, EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT, logWriter);
        }

        private class TestEntLibLoggerFactoryAdapter: EntLibLoggerFactoryAdapter
        {
            public LogEntry LastLogEntry;

            public TestEntLibLoggerFactoryAdapter(int defaultPriority, string exceptionFormat, LogWriter logWriter) 
                : base(defaultPriority, exceptionFormat, logWriter)
            {
            }

            protected override ILog CreateLogger(string name, LogWriter logWriter, EntLibLoggerSettings settings)
            {
                return new TestEntLibLogger(this, name, logWriter, settings);
            }

            private class TestEntLibLogger : EntLibLogger
            {
                private readonly TestEntLibLoggerFactoryAdapter owner;

                public TestEntLibLogger(TestEntLibLoggerFactoryAdapter owner, string category, LogWriter logWriter, EntLibLoggerSettings settings) 
                    : base(category, logWriter, settings)
                {
                    this.owner = owner;
                }

                protected override void WriteLog(LogEntry log)
                {
                    owner.LastLogEntry = log;
                    base.WriteLog(log);
                }
            }
        }

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