ASP.Net web.config refer to dll app app.config(entlib config) - error:Activation error occured while trying to get instance of type LogWriter, key

Topics: Logging Application Block
Jun 1, 2010 at 11:44 AM

Hi There,

  The asp.Net application uses the web.config file which refers to the app.config of the class dll(logging interface). The app.config is as below:

*********************************************************

ASP.Net web.config:

 <appSettings file="C:\Users\nischal.sathya\Documents\Visual Studio 2008\Projects\Facade\Facade\bin\Debug\Facade.dll.config">  
 </appSettings>

********************************************************

<?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="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                source="EnterpriseLibraryLogging" formatter="Text Formatter"
                log="Application" machineName="." traceOutputOptions="Timestamp, ProcessId, ThreadId, Callstack" />
            <add name="Message Queuing Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.MsmqTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.MsmqTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                queuePath=".\Private$\pocmsmq" formatter="Text Formatter"
                recoverable="true" useDeadLetterQueue="true" transactionType="Automatic"
                traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId" />
        </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="Event Log Listener" />
                    <add name="Message Queuing Trace Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <listeners>
                    <add name="Event Log Listener" />
                    <add name="Message Queuing Trace Listener" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
</configuration>

****************************************************

When the following code:

 

 public void Write(Guid ActivityId, Logtype Logtype, string Application, string payload, string message)
        {
            writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>(); //error is thrown here
            //traceMgr = EnterpriseLibraryContainer.Current.GetInstance<TraceManager>();
            LogEntry log = new LogEntry();
            MessageData msg = new MessageData();
            msg.activityId = new Guid();
            msg.application = "Perspective";
            msg.payload = "";

            log.Message = objecttoxmlstring(msg);
            log.Categories.Add("General");
            log.Priority = 1;

            writer.Write(log);

          }

**********************

when the following code is run this trows an error:

The type LogWriter cannot be constructed. You must configure the container to supply this value.

 System.InvalidOperationException: The type LogWriter cannot be constructed. You must configure the container to supply this value.

***************************************************

 

Also is there a way of selecting which log type to use such as event, DB , file, console and text which is already configured in the logging application(app.config).

[InvalidOperationException: The type LogWriter cannot be constructed. You must configure the container to supply this value.] Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.GuardTypeIsNonPrimitive(IBuilderContext context, SelectedConstructor selectedConstructor) +252 Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context) +356 Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +262 Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context, NamedTypeBuildKey buildKey) +187 Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +221 Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +262 Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +377 [ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The type LogWriter cannot be constructed. You must configure the container to supply this value.

 

 

 

 

 

 

 

 

 

Jun 2, 2010 at 12:43 AM

I'm actually not clear as to what is the relevance of the file specified in the appSettings' file attribute.  In my understanding, I believe it only points to another file that will add or override appsettings specified in the current configuration file. 

The config file which contains the logging configuration, is it from the web.config or from the other config file?

Anyway, if you want to point your logging configuration to a different file, you make use of the configSource attribute.  Note that this is not a feature of entlib but part of the .NET framework configuration system.

<loggingConfiguration configSource="differentLogging.config" />

There are other ways to make use of other configuration files other than the default, you can refer to Tom  Hollander's blog.

 

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