Retrieving LogWriter instance without configuration file

Topics: Logging Application Block
May 12, 2010 at 8:22 AM

Hi,

I would like to know what is the best way to retreive a LogWriter instance without a configuration file.

Our unit tests reside in more then one project and we don't want to create a seperate configuration file for each project all the time. Also, adding logging to code that is unit tested in a project without a configuration file would break the unit test. I've currently worked around this issue by introducing a wrapper that catches the ActivationException and creates an instance of the LogWriter using the LogWriterFactory.Create(IConfigurationSource source) with the source as a MockConfigurationSource. Ofcourse, I'm not happy with this solution.

Any advice would be very much appreciated.

Kind regards,

Arne Wauters

May 12, 2010 at 8:28 AM

What is your exact requirement?  You said that you want to retrieve a LogWriter without a config file, does this mean, you want to create it everytime you need to use it?  Please confirm which of these assumptions would you like to work on:

1.  No config file, thus, LogWriter would always be created whenever you need it

2.  Obtain an instance of LogWriter if there's a config file and create an instance of it if there is no config.

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com



May 12, 2010 at 8:38 AM

thanks for the swift reply.

I didn't really get the second option but the bottomline is this: if code is ran by the application there will be a configuration file and the LogWriter instance should be configured with it. If code is ran by unit tests however, there will be no configuration file and I would like to avoid getting an ActivationException because of this. I just want a 'default' LogWriter and no exception when there is no configuration file. I have the following but this will eat exceptions when there are errors in the configuration file which is not what I want. Is it really necessary to create a configuration file per project that has unit tests?

 

        private static LogWriter LogWriter
        {
            get
            {
                //lazy loading
                if (_logWriter == null)
                {
                    try
                    {
                        _logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
                    }
                    catch (Microsoft.Practices.ServiceLocation.ActivationException)
                    {
                        LogWriterFactory factory = new LogWriterFactory(new MockConfigurationSource());
                        _logWriter = factory.Create();
                    }
                }

                return _logWriter;
          }

May 12, 2010 at 9:04 AM

There's no default LogWriter.  You would always get it from a configsource either by resolving from the EnterpriseLibraryContainer.Current or by building a DictionaryConfigurationSource.

You can check for the existence of the logging section in your config:

IConfigurationSource configSource = new SystemConfigurationSource();
if (configSource.GetSection(LoggingSettings.SectionName) != null)
{
        //Resolve instance from EnterpriseLibraryContainer.Current
}
else
{
       //Create from MockConfigurationSource
}

Is this an acceptable solution for you?

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

 

May 12, 2010 at 9:44 AM

Yes, the solution fulfills all the requirements.

Thank you for the great support.

Aug 10, 2010 at 4:01 PM

I am building an Exchange Transport Agent and therefore no config file for logging. I'm using EntLib 5.0. The old way of doing it was to create an instance of LogWriter and pass to the ctor trace sources, log sources, etc. Now I'm being told that thew LogWriter ctor is protected.  How do what I need to do the new way using Unity?  Thanks!

Aug 11, 2010 at 1:12 AM

It is protected as it is now an abstract class in version 5.0.  The concrete implementation is the LogWriterImpl class which exposes public constructors.

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com