Description
Description
During implementation of #23937 it was discovered that having a config entry of filemappingsize
causes an exception and is not supported. It works fine in .NET Framework.
Reproduction Steps
Using a console app in Visual Studio in admin mode (admin required for perf counter creation):
string categoryName = "PerformanceCounterCategory_CategoryType_SingleInstance_Category";
CreateCategory(categoryName, PerformanceCounterCategoryType.SingleInstance);
PerformanceCounterCategory pcc = new PerformanceCounterCategory(categoryName);
PerformanceCounterCategory.Delete(categoryName);
void CreateCategory(string categoryName, PerformanceCounterCategoryType categoryType)
{
string counterName = categoryName.Replace("_Category", "_Counter");
// If the category already exists, delete it, then create it.
DeleteCategory(categoryName);
PerformanceCounterCategory.Create(categoryName, "description", categoryType, counterName, "counter description");
}
void DeleteCategory(string categoryName)
{
if (PerformanceCounterCategory.Exists(categoryName))
{
PerformanceCounterCategory.Delete(categoryName);
}
int tries = 0;
while (PerformanceCounterCategory.Exists(categoryName) && tries < 10)
{
System.Threading.Thread.Sleep(100);
tries++;
}
}
and a config file named, for example, ConsoleApp1.dll.config
and placed in the bin location next to ConsoleApp1.dll
:
<configuration>
<system.diagnostics>
<performanceCounters filemappingsize="6524288" />
</system.diagnostics>
</configuration>
Causes
System.Configuration.ConfigurationErrorsException
HResult=0x80131902
Message=Configuration system failed to initialize
Source=System.Configuration.ConfigurationManager
StackTrace:
at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey) in /_/src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/ClientConfigurationSystem.cs:line 164
at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName) in /_/src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/ClientConfigurationSystem.cs:line 201
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName) in /_/src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/ClientConfigurationSystem.cs:line 42
at System.Configuration.ConfigurationManager.GetSection(String sectionName) in /_/src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/ConfigurationManager.cs:line 154
at System.Diagnostics.DiagnosticsConfiguration.GetConfigSection() in /_/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/DiagnosticsConfiguration.cs:line 63
at System.Diagnostics.DiagnosticsConfiguration.Initialize() in /_/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/DiagnosticsConfiguration.cs:line 95
at System.Diagnostics.DiagnosticsConfiguration.get_PerformanceCountersFileMappingSize() in /_/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/DiagnosticsConfiguration.cs:line 34
at System.Diagnostics.SharedPerformanceCounter.GetFileMappingSizeFromConfig() in /_/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/SharedPerformanceCounter.cs:line 731
at System.Diagnostics.SharedPerformanceCounter.GetCategoryData() in /_/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/SharedPerformanceCounter.cs:line 671
at System.Diagnostics.SharedPerformanceCounter..ctor(String catName, String counterName, String instanceName, PerformanceCounterInstanceLifetime lifetime) in /_/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/SharedPerformanceCounter.cs:line 96
at System.Diagnostics.SharedPerformanceCounter..ctor(String catName, String counterName, String instanceName) in /_/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/SharedPerformanceCounter.cs:line 89
at System.Diagnostics.SharedPerformanceCounter.RemoveAllInstances(String categoryName) in /_/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/SharedPerformanceCounter.cs:line 1389
at System.Diagnostics.PerformanceCounterCategory.Delete(String categoryName) in /_/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/PerformanceCounterCategory.cs:line 392
at ConsoleApp54.Program.<DoIt>g__DeleteCategory|1_1(String categoryName) in C:\Users\sharter\source\repos\ConsoleApp54\Program.cs:line 40
at ConsoleApp54.Program.<DoIt>g__CreateCategory|1_0(String categoryName, PerformanceCounterCategoryType categoryType) in C:\Users\sharter\source\repos\ConsoleApp54\Program.cs:line 32
at ConsoleApp54.Program.DoIt() in C:\Users\sharter\source\repos\ConsoleApp54\Program.cs:line 21
at ConsoleApp54.Program.Main(String[] args) in C:\Users\sharter\source\repos\ConsoleApp54\Program.cs:line 14
This exception was originally thrown at this call stack:
System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(bool) in ConfigurationSchemaErrors.cs
System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(System.Configuration.ConfigurationSchemaErrors) in BaseConfigurationRecord.cs
System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors() in BaseConfigurationRecord.cs
System.Configuration.ClientConfigurationSystem.EnsureInit(string) in ClientConfigurationSystem.cs
Inner Exception 1:
ConfigurationErrorsException: Unrecognized configuration section system.diagnostics. (C:\Users\sharter\source\repos\ConsoleApp54\bin\Debug\net7.0\ConsoleApp54.dll.config line 2)
Expected behavior
No exception; the file mapping size applied. Note that there is not a public API to read the applied value, so that is somewhat unfortunate for testing purposes and validation.
Actual behavior
Exception saying ConfigurationErrorsException: Unrecognized configuration section system.diagnostics.
Regression?
I don't believe so. The System.Diagnostics.ConfigurationManager
assembly is missing the System.Diagnostics entry in the ImplicitMachineConfigHost
class which is causing the exception.
Known Workarounds
No response
Configuration
No response
Other information
This issue won't repro after #73087 is in since that removes this support because fixing it requires new APIs or prevents the TraceSource functionality in that PR from working properly since the implementation on the PerfCounter side assumes no other entries in the System.Diagnostics
section in the config file.
Adding support for this will likely require new APIs as temporary implemented in a5ac25d
namespace System.Diagnostics
{
public sealed partial class PerfCounterSettings : System.Configuration.ConfigurationElement
{
public PerfCounterSettings() { }
public int FileMappingSize { get { throw null; } }
protected override System.Configuration.ConfigurationPropertyCollection Properties { get { throw null; } }
}
public sealed partial class SystemDiagnosticsSection : System.Configuration.ConfigurationSection
{
public SystemDiagnosticsSection() { }
public System.Diagnostics.PerfCounterSettings PerfCounterSettings { get { throw null; } }
protected override System.Configuration.ConfigurationPropertyCollection Properties { get { throw null; } }
protected override void InitializeDefault() { }
}
}