Activation error occured while trying to get instance of type LogWriter, key ""?

James123 picture James123 · Oct 18, 2011 · Viewed 55.9k times · Source

I have asp.net web site. I added reference of Microsoft.Practices.EnterpriseLibrary.Logging.dll to the site.

in web.cofig file, I defined like 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="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
        fileName="C:\Temp\RollingFlatFile.log" footer="----------------------------------------"
        formatter="Text Formatter" header="----------------------------------------"
        rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="20"
        timeStampPattern="yyyy-MM-dd" maxArchivedFiles="3" traceOutputOptions="None"
        filter="All" />
      </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
        template="Timestamp: {timestamp(local)}{tab}Message: {message}{tab}Category: {category}{tab}Priority: {priority}{tab}EventId: {eventid}{tab}ActivityId: {property(ActivityId)}{tab}Severity: {severity}{tab}Title:{title}{tab}"
        name="Brief Format Text" />
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
        template="Timestamp: {timestamp}{tab}Message: {message}{tab}Category: {category}{tab}Priority: {priority}{tab}EventId: {eventid}{tab}Severity: {severity}{tab}Title: {title}{tab}Activity ID: {property(ActivityId)}{tab}Machine: {localMachine}{tab}App Domain: {localAppDomain}{tab}ProcessId: {localProcessId}{tab}Process Name: {localProcessName}{tab}Thread Name: {threadName}{tab}Win32 ThreadId:{win32ThreadId}{tab}Extended Properties: {dictionary({key} - {value}{tab})}"
        name="Text Formatter" />
    </formatters>
    <logFilters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
        minimumPriority="2" maximumPriority="99" name="Priority Filter" />
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
        enabled="true" name="LogEnabled Filter" />
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
        categoryFilterMode="AllowAllExceptDenied" name="Category Filter">
        <categoryFilters>
          <add name="BlockedByFilter" />
        </categoryFilters>
      </add>
    </logFilters>
    <categorySources>
      <add switchValue="All" name="Important">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="UnprocessedFlatFile" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
</configuration>

When I run the code exception happening below place.

defaultWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
//Activation error occured while trying to get instance of type LogWriter, key ""

How to resolve this?

Answer

Randy supports Monica picture Randy supports Monica · Oct 19, 2011

Your full config references trace listeners that are not defined. This is causing the Enterprise Library runtime to throw a runtime exception when it tries to resolve those references.

I commented out the unused listeners (and changed the default category to Important) and it is working:

<?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="Logging Application Block" tracingEnabled="true"
      defaultCategory="Important" logWarningsWhenNoCategoriesMatch="true">
        <listeners>
            <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
              listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
              fileName="C:\Temp\RollingFlatFile.log" footer="----------------------------------------"
              formatter="Text Formatter" header="----------------------------------------"
              rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="20"
              timeStampPattern="yyyy-MM-dd" maxArchivedFiles="3" traceOutputOptions="None"
              filter="All" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
              template="Timestamp: {timestamp(local)}{tab}Message: {message}{tab}Category: {category}{tab}Priority: {priority}{tab}EventId: {eventid}{tab}ActivityId: {property(ActivityId)}{tab}Severity: {severity}{tab}Title:{title}{tab}"
              name="Brief Format Text" />
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
              template="Timestamp: {timestamp}{tab}Message: {message}{tab}Category: {category}{tab}Priority: {priority}{tab}EventId: {eventid}{tab}Severity: {severity}{tab}Title: {title}{tab}Activity ID: {property(ActivityId)}{tab}Machine: {localMachine}{tab}App Domain: {localAppDomain}{tab}ProcessId: {localProcessId}{tab}Process Name: {localProcessName}{tab}Thread Name: {threadName}{tab}Win32 ThreadId:{win32ThreadId}{tab}Extended Properties: {dictionary({key} - {value}{tab})}"
              name="Text Formatter" />
        </formatters>
        <logFilters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
              minimumPriority="2" maximumPriority="99" name="Priority Filter" />
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
              enabled="true" name="LogEnabled Filter" />
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
              categoryFilterMode="AllowAllExceptDenied" name="Category Filter">
                <categoryFilters>
                    <add name="BlockedByFilter" />
                </categoryFilters>
            </add>
        </logFilters>
        <categorySources>
            <add switchValue="All" name="Important">
                <listeners>
                    <!--<add name="Formatted EventLog TraceListener" />-->
                    <add name="Rolling Flat File Trace Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category">
                <listeners>
                    <!--<add name="UnprocessedFlatFile" />-->
                </listeners>
            </notProcessed>
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <listeners>
                    <!--<add name="Formatted EventLog TraceListener" />-->
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
</configuration>

The clue is in the inner exception. At the end of the InnerException.Message it says:

Resolving System.Diagnostics.TraceListener,Formatted EventLog TraceListener

The above config is in app.config. Then Program.cs contains:

class Program
{
    static void Main(string[] args)
    {
        LogWriter logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
        logWriter.Write("Test");
     }
}