Getting an "Activation error occured while trying to get instance of type LogWriter, key "" -

Topics: Building and extending application blocks, Logging Application Block
Oct 19, 2010 at 8:57 AM

Hi All,

I was just exploring the Logging application block and tried adding it to my project, but i keep getting the mentioned error in the following line:

LogWriter writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
writer.Write("My Log message");



I am using MS Enterprise Library 5 and VS 2010.

I added a new app.config file to my project and opened it using the Enterprise Library V5 Configuration tool. I then deleted the Event log trace listener that was already there and added a Flat file trace listener. Then i set the listeners for all the catergories (General, All Events, Unprocessed categories & Logging Errors and Warnings) to Flat file trace listener. Saved the config file as shown below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
            <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="C:\ExcelFiles\trace.log" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
                name="Text Formatter" />
        </formatters>
        <categorySources>
            <add switchValue="All" name="General">
                <listeners>
                    <add name="Flat File Trace Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events">
                <listeners>
                    <add name="Flat File Trace Listener" />
                </listeners>
            </allEvents>
            <notProcessed switchValue="All" name="Unprocessed Category">
                <listeners>
                    <add name="Flat File Trace Listener" />
                </listeners>
            </notProcessed>
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <listeners>
                    <add name="Flat File Trace Listener" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
</configuration>

 I have created the trace.log file in the appropriate dir as well.Now when ever i come to the line of code given above, i get the error message.

I have copied the following dlls into my project bin folder and added references and include statements as well for them:

Microsoft.Practices.EnterpriseLibrary.Logging.dll
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.dll

 

Can any one please let me know what i am doing wrong or what else needs to be done.

 

Thanks and Regards,

Amit Bansal.

Oct 19, 2010 at 2:17 PM

Is your app.config file part of your executable project?  Usually, people thought the configuration should be defined in whatever project the logging code is in so they put config files in class libraries.  Class libraries don't own configuration files so it should always be part of the host project.

 

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

Oct 19, 2010 at 3:07 PM

do I need to add this Dll? for logging?

 

Microsoft.Practices.ServiceLocation.dll

Oct 19, 2010 at 11:18 PM

It's not necessary if you use the static Logger class when logging

Logger.Write("sample message", "someCategory");

instead of resolving an instance of LogWriter

LogWriter logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

logWriter.Write("sample message", "someCategory");

 

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

Oct 20, 2010 at 9:28 AM

Hi Sarah,

Thanks for the reply. Indeed i have added the config file to a class library project and this was the cause of the error. I created a temporary host windows application and used the class library there, it works fine.

My problem however is that my entire project is a class library without any host application. I mean i am registering this class library as a bandobject for Internet Explorer to diplay my custom toolbar in IE. Does this mean that i cannot  use logging in this scenario or do i have any way to implement logging for this type of applications?

Thanks and Regards,

Amit Bansal.

Oct 20, 2010 at 11:59 AM

You can make use of the LogWriterFactory class passing an instance of FileConfigurationSource pointing to the config you want to use.  You can refer to this blog for more information.

 

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