Problem with Custom TraceListener and Unit Tests

Topics: Data Access Application Block, Logging Application Block
Mar 3 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.
Editor
Mar 4 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
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Mar 5 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
2.MoveNext()
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.
Editor
Mar 7 at 10:45 AM
That is very weird. I did see that some people had the same exception when running unit tests (http://stackoverflow.com/questions/14333264/common-language-runtime-detected-an-invalid-program-error-in-unit-testing , http://stackoverflow.com/questions/14578355/system-invalidprogramexception-when-executing-unit-tests-in-mstest-after-microso ) with the culprit being a security update (http://support.microsoft.com/kb/2742595).

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

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to