7

On a build machine running Visual Studio Pro 2013 12.0.21005.1, I have a unit test project that fails to read its App.config file properly (if at all). The following returns null:

System.Configuration.ConfigurationManager.GetSection( "loggingConfiguration" )

It isn't just the logging configuration section that fails to get read; any attempt to read any section results in a null value.

On my laptop running Visual Studio Pro 2013 12.0.31101.00 Update 4, the unit test project reads the App.config just fine, and the call above returns the expected object.

My XML looks like this:

<?xml version="1.0"?>
<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" />
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
    <section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>

<loggingConfiguration name="" tracingEnabled="false" defaultCategory="Verbose" logWarningsWhenNoCategoriesMatch="false">
    <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="MyCompany" formatter="Text Formatter" log="Application" />
      <add name="Rolling Flat File Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" fileName="Service.log" formatter="Text Formatter" rollFileExistsBehavior="Increment" rollInterval="Day" />
      <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" type="MyCompany.Core.Instrumentation.ConsoleTraceListener, MyCompany.Core" formatter="Debug Formatter" traceOutputOptions="None" name="Debug Console Listener" />
    </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(local)}{newline}&#xA;Title: {title}{newline}&#xA;Message: {message}{newline}&#xA;Severity: {severity}{newline}&#xA;Priority: {priority}{newline}&#xA;Categories: {category}{newline}&#xA;EventId: {eventid}{newline}&#xA;Machine: {localMachine}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}" name="Text Formatter" />
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="{timestamp(local:FixedFormatUSDate)} {timestamp(local:FixedFormatTime)} [{severity} P{priority} T{win32ThreadId}/'{threadName}']   {title}: {message} [{category}]" name="Debug Formatter" />
    </formatters>
    <logFilters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" categoryFilterMode="AllowAllExceptDenied" name="Category Filter">
        <categoryFilters>
        </categoryFilters>
      </add>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" maximumPriority="4" name="Priority Filter" />
    </logFilters>
    <categorySources>
      <add switchValue="Information" name="MyCompany.Core.Configuration">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="ServiceExceptionPolicy">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Critical">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Error">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Warning">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Information">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Verbose">
        <listeners>
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="Off" name="All Events" />
      <notProcessed switchValue="Off" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="ServiceExceptionPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" logCategory="ServiceExceptionPolicy" eventId="100" severity="Error" title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" priority="0" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
  <dataConfiguration defaultDatabase="SOMEDATABASE" />
  <connectionStrings>
  <!-- some connection strings -->
  </connectionStrings>
</configuration>

Anyone know any reason this would fail in one environment but not the other?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
MiloDC
  • 2,373
  • 1
  • 16
  • 25
  • It sounds like your local test is running in the build directory but the build machine is running tests in a deployment folder and the config file was not deployed. – Randy Levy Jul 24 '15 at 19:54
  • I checked, the .config file is definitely there with all of the DLLs, where it's supposed to be. – MiloDC Jul 24 '15 at 20:21
  • 1
    Researched this some more. Apparently, occasionally failing to recognize App.config is a known issue with MSTest -- an outdated test runner (since 2010 or 2011, I think) that our continuous integration is apparently still using. For now, I'm just going to ignore the tests in question, until we replace MSTest in our CI pipeline with VSTest (which update I've asked the people responsible to effect). – MiloDC Jul 28 '15 at 00:51
  • Ah... VS 2013. It had so many issues, I dropped using it. Get 2012 or 2015. I remember many issues with 2013. Certain projects did not run well, etc. – T.S. Jan 29 '22 at 23:58
  • Have you checked your setup with another section retrieval? Like: `System.Configuration.ConfigurationManager.GetSection( "exceptionHandling")` – Peter Csala Feb 01 '22 at 15:51

1 Answers1

0

This requires a custom configuration:

<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" />

Take a look inside type which points to the custom configuration handler. My guess is the DLL reference is missing from the test project.

beautifulcoder
  • 10,832
  • 3
  • 19
  • 29