Problem with Custom TraceListener and Unit Tests

Topics: Data Access Application Block, Logging Application Block
Mar 3, 2014 at 8:50 PM
Hello again!

I am in the process of trying to unit test logging to the database, and have come across an issue. When I instantiate my logger, the following is run inside the constructor:
LogWriter _lw;
public EntlibLogger()

            DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
            LogWriterFactory logWriterFactory = new LogWriterFactory();
            _lw = logWriterFactory.Create();
Upon reaching the line where we instantiate our LogWriterFactory object, I get the dreaded error:

"Invalid TraceListenerData type in configuration 'listenerDataType="NYCB.Mortgage.Services.Logging.TraceListeners.FormattedOracleTraceListenerData, NYCB.Mortgage.Services"'.

Now, I have made sure that the namespace for the trace listener above is correct in my config file, and I have made sure that I am referencing the Logging.Database dll (it is correctly deploying). Outside of unit tests, the logger works correctly, so I'm assuming there is something special I need to do for unit testing? Not quite sure where to go at this point.... Any tips/suggestions? Thanks in advance.
Mar 4, 2014 at 2:17 PM
It sounds like the assembly, NYCB.Mortgage.Services, is not being deployed as part of the unit test. You can set the DeploymentSettings or reference the type in the code (e.g. Type t = typeof(NYCB.Mortgage.Services.Logging.TraceListeners.FormattedOracleTraceListenerData) ).

Randy Levy
Enterprise Library support engineer
Support How-to
Mar 5, 2014 at 3:10 PM
Thanks Randy, that worked great. However, I now have a new issue. When I run unit tests, I'm getting the following error:

Test method NYCB.Mortgage.Tests.EntlibLoggerTests.GetNewActivityTrackingId_WhenLoggerIsDisposed_ThrowsObjectDisposedException threw exception System.InvalidProgramException, but exception System.ObjectDisposedException was expected. Exception message: System.InvalidProgramException: Common Language Runtime detected an invalid program.

at Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter..ctor(String template, IDictionary2 extraTokenHandlers)
at Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter..ctor(String template)
at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TextFormatterData.BuildFormatter()
at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TraceListenerData.BuildFormatterSafe(LoggingSettings settings, String formatterName)
at NYCB.Mortgage.Services.Logging.TraceListeners.FormattedOracleTraceListenerData.CoreBuildTraceListener(LoggingSettings settings) in FormattedOracleTraceListenerData.cs: line 143
at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TraceListenerData.BuildTraceListener(LoggingSettings settings)
at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings.BuildTraceListener(String name, ConfigurationElement requestor)
at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings.<>c__DisplayClass8.<BuildTraceSource>b__7(TraceListenerReferenceData tln)
at System.Linq.Enumerable.WhereSelectEnumerableIterator
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at Microsoft.Practices.EnterpriseLibrary.Logging.LogSource..ctor(String name, IEnumerable1 traceListeners, SourceLevels level, Boolean autoFlush)
at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings.BuildTraceSource(TraceSourceData tsd, Dictionary
2 listeners)
at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings.<>c__DisplayClass5.<BuildLogWriter>b__1(TraceSourceData tsd)
at System.Linq.Enumerable.ToDictionary(IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer)
at System.Linq.Enumerable.ToDictionary(IEnumerable1 source, Func2 keySelector, Func`2 elementSelector)
at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings.BuildLogWriter()
at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterFactory.LogWriterConfigurationBuilder.Create()
at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterFactory.Create()
at NYCB.Mortgage.Logging.Entlib.EntlibLogger..ctor() in EntlibLogger.cs: line 62
at NYCB.Mortgage.Tests.EntlibLoggerTests.GetNewActivityTrackingId_WhenLoggerIsDisposed_ThrowsObjectDisposedException() in EntlibLoggerTests.cs: line 508

It appears the problem lies with the CoreBuildTraceListener method in my FormattedOracleTraceListenerData file. The error shows that there's a problem when I'm instantiating the formatter variable in the method. I know the TextFormatter is written correctly in the config file, and I believe I'm calling it correct in the method. Here is the CoreBuildTraceListener:
protected override TraceListener CoreBuildTraceListener(LoggingSettings settings)
            Database database = new DatabaseProviderFactory().Create(this.DatabaseInstanceName);
            var formatter = this.BuildFormatterSafe(settings, this.Formatter);

            return new FormattedOracleTraceListener(database, writeLogStoredProcNameProperty, addCategoryStoredProcNameProperty, formatter);
Another weird thing to note....
These unit tests actually pass when I debug them and step through. But when I just run them, they all fail with the same error above. Any ideas? Thanks again for any info you can provide, and let me know if I can provide any more info on my side.
Mar 7, 2014 at 10:45 AM
That is very weird. I did see that some people had the same exception when running unit tests ( , ) with the culprit being a security update (

I'm not sure if that is the issue but maybe it helps.

Randy Levy
Enterprise Library support engineer
Support How-to
Aug 20, 2015 at 11:41 PM
I am also getting this weird error. I have set up a TFS Unit Test Build. In my test I have below line.

Type msEntLibLogDBType = typeof(Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener);

As per your note above. But I am still getting this error.

Class Initialization method RightsLine.Test.WCFServiceTest.WorkFlowVerification.UserRelatedWorkflowTest.Setup threw exception. System.Configuration.ConfigurationErrorsException: System.Configuration.ConfigurationErrorsException: Invalid TraceListenerData type in configuration 'listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database"

Not sure why? This used to work before but I am getting error now again.

I am using VS 2015 and TFS 2015 is also running the test build. How could i use DeploymentSettings to make sure this dll will be there in output folder.
I checked the output folder, and the dll is not there. Although same dll is there in the Binaries folder on my TFS Server.

Please help.