【发布时间】:2015-10-15 13:45:43
【问题描述】:
在运行 Visual Studio Pro 2013 12.0.21005.1 的构建机器上,我有一个单元测试项目无法正确读取其 App.config 文件(如果有的话)。以下返回null:
System.Configuration.ConfigurationManager.GetSection( "loggingConfiguration" )
读取失败的不仅仅是日志配置部分;任何读取任何部分的尝试都会导致 null 值。
在我运行 Visual Studio Pro 2013 12.0.31101.00 Update 4 的笔记本电脑上,单元测试项目读取 App.config 就好了,上面的调用返回了预期的对象。
我的 XML 如下所示:
<?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}
Title: {title}{newline}
Message: {message}{newline}
Severity: {severity}{newline}
Priority: {priority}{newline}
Categories: {category}{newline}
EventId: {eventid}{newline}
Machine: {localMachine}{newline}
ProcessId: {localProcessId}{newline}
Process Name: {localProcessName}{newline}
Win32 ThreadId:{win32ThreadId}{newline}
Thread Name: {threadName}{newline}
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 & 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>
有人知道这会在一种环境中失败但在另一种环境中失败的任何原因吗?
【问题讨论】:
-
听起来您的本地测试正在构建目录中运行,但构建机器正在部署文件夹中运行测试并且配置文件未部署。
-
我检查了,.config 文件肯定在所有 DLL 中,它应该在哪里。
-
对此进行了更多研究。显然,偶尔无法识别 App.config 是 MSTest 的一个已知问题——我们的持续集成显然仍在使用一个过时的测试运行程序(我认为是从 2010 年或 2011 年开始)。现在,我将忽略有问题的测试,直到我们用 VSTest 替换 CI 管道中的 MSTest(我已要求负责人执行此更新)。
-
啊... VS 2013。它有很多问题,我放弃了使用它。获取 2012 或 2015。我记得 2013 的许多问题。某些项目运行不佳,等等。
-
您是否通过其他部分检索检查了您的设置?喜欢:
System.Configuration.ConfigurationManager.GetSection( "exceptionHandling")
标签: c# unit-testing visual-studio-2013 app-config enterprise-library-5