Common.Logging

Topics: Building and extending application blocks, Logging Application Block
Sep 1, 2010 at 12:05 PM

Hi I'm trying to get EntLib 5.0 working with the common.logging project that was built with Entlib 4.1 there are 2 classes that are not working beacuse of changes in Entlib 5.0. Can anyone point me in the right direction as I'm not sure what to do! Thanks in advance.

Regards

Tobi.

using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.ObjectBuilder2;

namespace Common.Logging.EntLib
{
    ///<summary>
    /// This type supports the Enterprise Library infrastructure and is not intended to be used directly from your code. 
    /// Represents the process to build a <see cref="CommonLoggingEntlibTraceListener"/> described by a 
    /// <see cref="CommonLoggingEntlibTraceListenerData"/> configuration object. 
    ///</summary>
    public class CommonLoggingEntlibTraceListenerAssembler : TraceListenerData
    {
        ///<summary>
        /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code. 
        /// Builds a <see cref="CommonLoggingEntlibTraceListener"/> described by a 
        /// <see cref="CommonLoggingEntlibTraceListenerData"/> configuration object. 
        ///</summary>
        public override TraceListener Assemble(IBuilderContext context, TraceListenerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            CommonLoggingEntlibTraceListenerData data = (CommonLoggingEntlibTraceListenerData) objectConfiguration;
            ILogFormatter formatter = base.GetFormatter(context, data.Formatter, configurationSource, reflectionCache);

            CommonLoggingEntlibTraceListener listener = (CommonLoggingEntlibTraceListener)System.Activator.CreateInstance(objectConfiguration.Type, new object[] { objectConfiguration, formatter });
            return listener;
        }
    }
}

using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;

namespace Common.Logging.EntLib
{
    /// <summary>
    /// Represents the configuration settings that describe a <see cref="CommonLoggingEntlibTraceListener"/>.
    /// </summary>
    [Assembler(typeof(CommonLoggingEntlibTraceListenerAssembler))]
    public class CommonLoggingEntlibTraceListenerData : TraceListenerData
    {
        private const string loggerNameFormatProperty = "loggerNameFormat";

        /// <summary>
        /// The logger name format to use for creating logger names from <see cref="NamedConfigurationElement.Name"/> and 
        /// </summary>
        [ConfigurationProperty(loggerNameFormatProperty, IsRequired = false)]
        public string LoggerNameFormat
        {
            get { return (string) base[loggerNameFormatProperty ] ; }
            set { base[loggerNameFormatProperty] = value; }
        }

        /// <summary>
        /// Gets or Sets the formatter name
        /// </summary>
        [ConfigurationProperty("formatter", IsRequired = false)]
        public string Formatter
        {
            get
            {
                return (string)base["formatter"];
            }
            set
            {
                base["formatter"] = value;
            }
        }
    }
}
Sep 2, 2010 at 4:02 AM

The Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder assembly does not exist in entlib5 anymore that is why reference to classes like AssemblerAttribute and ConfigurationReflectionCache will have a compile error. Unfortunately, I'm not aware of any equivalent classes for this in entlib5 and as far as I know every detailed change to the Entlib internals was intentionally not documented (see Chris Tavares take on this http://entlib.codeplex.com/Thread/View.aspx?ThreadId=218407). Given this, we'll just inquire to entlib team if there is any equivalent classes for those you're still using.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 9, 2010 at 7:56 PM

Hello,

I am also facing the same issue when migrating from enterprise library 3.1 to 5. Can anyone tell us the solution for this?

Thanks,

Raja

Dec 14, 2010 at 1:21 AM

Creating custom providers in EntLib 5.0 is easier compared to the lower versions.  The instructions are included in the documentation, you can refer here.

If you only want basic integration with the configuration tool, you just need to decorate your custom provider with the ConfigurationElementType attribute specifying the corresponding class for the application block.  The class you will use is also documented in the Enterprise Library Extension Points section.  For example, for a custom trace listener, your attribute should look like [ConfigurationElementType(typeof(CustomTraceListenerData))]

If you want full integration, you need to create a class which subclass from the corresponding configuration element base class.  That class is also provided in the Enterprise Library Extension Points section.  You would then use that class as the type for the ConfigurationElementType attribute.

We have sample custom database trace listener, email us if you want a copy.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com