objectbuilder2 error for custome mockTraceListener

Topics: Logging Application Block
Mar 31, 2010 at 7:33 PM

Hi all,

I am currently using the logging application block from EntLib 4.1.  I created a MockTraceListener for unit testing.  Here is the code.

 

[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class MockTraceListener : CustomTraceListener
{
private readonly static List<LogEntry> logEntries = new List<LogEntry>();

private readonly static List<string> logMessages = new List<string>();

public override void Write(string message)
{
MockTraceListener.logMessages.Add(message);
}

public override void WriteLine(string message)
{
MockTraceListener.logMessages.Add(message);
}

public override void TraceData(TraceEventCache eventCache, string source,
TraceEventType eventType, int id, object data)
{
LogEntry le = data as LogEntry;
if (le != null)
{
MockTraceListener.logEntries.Add(le);
if (this.Formatter != null)
{
this.Write(this.Formatter.Format(le));
return;
}
}
base.TraceData(eventCache, source, eventType, id, data);
}

internal static IList<string> GetLogMessages()
{
return new ReadOnlyCollection<string>(MockTraceListener.logMessages);
}

internal static IList<LogEntry> GetLogEntries()
{
return new ReadOnlyCollection<LogEntry>(MockTraceListener.logEntries);
}

internal static void Reset()
{
MockTraceListener.logEntries.Clear();
MockTraceListener.logMessages.Clear();
}

}

and here is the configuration

<loggingConfiguration defaultCategory="" tracingEnabled="false">
<specialSources>
<errors name="errors" switchValue="All">
<listeners>
<add name="Event Log Destination"/>
</listeners>
</errors>
<allEvents switchValue="All" name="All Events">
<listeners>
<add name="MockListener" />
</listeners>
</allEvents>
</specialSources>
<listeners>
<add name="Event Log Destination" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging" source="Enterprise Library Logging" formatter="Default Formatter"/>
<add name="Flat File Destination" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging" fileName="trace.log"/>
<add name="HitCounter Log Destination" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.MsmqTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.MsmqTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging" traceOutputOptions="None" queuePath=".\Private$\hitcountqueue" formatter="Binary Formatter" messagePriority="Normal" timeToReachQueue="49710.06:28:15" timeToBeReceived="49710.06:28:15" recoverable="false" useAuthentication="false" useDeadLetterQueue="false" useEncryption="false" transactionType="None"/>
<add name="MockListener" type="Work.UnitTests.MockTraceListener, Work.UnitTests" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging" />

</listeners>
<formatters>
<add name="Default Formatter" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging" template="Timestamp: {timestamp}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Application Domain: {appDomain}&#xA;Process Id: {processId}&#xA;Process Name: {processName}&#xA;Win32 Thread Id: {win32ThreadId}&#xA;Thread Name: {threadName}&#xA;Extended Properties: {dictionary({key} - {value}&#xA;)}"/>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.BinaryLogFormatter, Microsoft.Practices.EnterpriseLibrary.Logging" name="Binary Formatter"/>
</formatters>
</loggingConfiguration>

When I try to run my unit test case, it gives me the following error.

 Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: The [Assembler] attribute is not set in the configuration object type Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TraceListenerData. (Strategy type ConfiguredObjectStrategy, index 2) --->  System.InvalidOperationException: The [Assembler] attribute is not set in the configuration object type Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TraceListenerData..

and the stacktraces is

GetAssemblerAttribute(Type type)
GetAssembler(TConfiguration objectConfiguration)
Create(IBuilderContext context, TConfiguration objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.TraceListenerCustomFactory.Create(IBuilderContext context, TraceListenerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
Create(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.TraceListenerCustomFactory.Create(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache, TraceListenerCache traceListenersCache)
Microsoft.Practices.EnterpriseLibrary.Logging.LogSourceCustomFactory.Create(IBuilderContext context, TraceSourceData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache, TraceListenerCache traceListenersCache)
Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterStructureHolderCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.PreBuildUp(IBuilderContext context)
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing)
Microsoft.Practices.ObjectBuilder2.Builder.BuildUp[TTypeToBuild](IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing)
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IReadWriteLocator locator, ILifetimeContainer lifetimeContainer, IConfigurationSource configurationSource)
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IConfigurationSource configurationSource)
Microsoft.Practices.EnterpriseLibrary.Logging.Logger.get_Writer()
Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(LogEntry log)
.....

Does anyone have any idea of what went wrong here?

 

Apr 5, 2010 at 4:54 AM

Hi,

Where is your MockTraceListener.cs file located? Is the unit test code that uses the config and the MockTraceListener.cs resides in the same project?

If it's not, try referencing your MockTraceListener project output or DLL into your Test code/project. Then try running the test again.

Let me know if this resolves your issue and if there are questions.

HTH,

~ginkapitan

Apr 5, 2010 at 4:14 PM
Hi ginkapitan, The MockTraceListener.cs is in the same direction of the unit testing code that I am trying to execute. thanks, Herman
Apr 6, 2010 at 3:51 AM

Hi Herman,

I tried to just copy and paste your logging config and happen that it can't open up properly with VS EntLib Config tool.

Also, by looking at the EntLib assembly you use in your config the full name of the type wasn't even specified with its full name. With this, I have come to an assumption that the configuration was all made by hand. Let me know if I may be mistaken.

Given this, I highly recommend using either the VS or the standalone EntLib config tool in adding a Logging Application Block in your application.

Your config should may look like the below sample once you're done. HTH

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Work.UnitTests.MockTraceListener, Work.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        name="Custom Trace Listener" initializeData="" formatter="Text Formatter" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Custom Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings" />
    </specialSources>
  </loggingConfiguration>
</configuration>

~ginkapitan

 

 

Apr 7, 2010 at 4:43 PM
Hi ginkapitan, The configuration file indeed made by hand, but it was used in another project, so it work then. The only thing that I added is the mockListener for unit testing. Anyways, I used your configuration file and it worked! The only thing that was wrong with my configuration file is that I didn't specified the listenDataType correctly. Thank you very much in going above and beyond on this problem, very much appreciated! Herman