Skip to content

Commit

Permalink
Fix System.Diagnostics.PerformanceCounter diag overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
steveharter committed Aug 9, 2022
1 parent 3159c85 commit a5ac25d
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1578,15 +1578,22 @@ public ProviderException(string message) { }
public ProviderException(string message, System.Exception innerException) { }
}
}
#if NET7_0_OR_GREATER
namespace System.Diagnostics
{
public static partial class TraceConfiguration
public sealed partial class PerfCounterSettings : System.Configuration.ConfigurationElement
{
public static void Register() { }
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() { }
}
}
#endif
namespace System.Drawing.Configuration
{
public sealed partial class SystemDrawingSection : System.Configuration.ConfigurationSection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<Nullable>disable</Nullable>
Expand All @@ -8,6 +8,7 @@
<ItemGroup>
<Compile Include="System.Configuration.ConfigurationManager.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'" />
<Compile Include="$(CommonPath)System\Obsoletions.cs" Link="Common\System\Obsoletions.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'" />
<Compile Include="System.Configuration.ConfigurationManager.netcoreapp.cs" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))" />
<Compile Include="System.Configuration.ConfigurationManager.netframework.cs" Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// ------------------------------------------------------------------------------
// Changes to this file must follow the https://aka.ms/api-review process.
// ------------------------------------------------------------------------------

namespace System.Diagnostics
{
public static partial class TraceConfiguration
{
public static void Register() { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ System.Configuration.ConfigurationManager</PackageDescription>
<Compile Include="System\Configuration\SettingsPropertyValue.cs" />
<Compile Include="System\Configuration\SettingsSerializeAs.cs" />
<Compile Include="System\Configuration\NoSettingsVersionUpgradeAttribute.cs" />
<Compile Include="System\Diagnostics\PerfCounterSettings.cs" />
<Compile Include="System\Diagnostics\SystemDiagnosticsSection.cs" />
<Compile Include="System\UriIdnScope.cs" />
<Compile Include="$(CommonPath)System\IO\TempFileCollection.cs" Link="Common\System\IO\TempFileCollection.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs" Link="Common\System\Obsoletions.cs" />
Expand All @@ -269,13 +271,13 @@ System.Configuration.ConfigurationManager</PackageDescription>
<Compile Include="System\Diagnostics\ListenerElementsCollection.cs" />
<Compile Include="System\Diagnostics\SourceElementsCollection.cs" />
<Compile Include="System\Diagnostics\SwitchElementsCollection.cs" />
<Compile Include="System\Diagnostics\SystemDiagnosticsSection.cs" />
<Compile Include="System\Diagnostics\SystemDiagnosticsSection.netcoreapp.cs" />
<Compile Include="System\Diagnostics\TraceConfiguration.cs" />
<Compile Include="System\Diagnostics\TraceSection.cs" />
<Compile Include="System\Diagnostics\TypedElement.cs" />
<Compile Include="System\Diagnostics\TraceUtils.cs" />
</ItemGroup>

<!-- Since this package is compatible with .NETStandard, it must also ensure its .NETFramework and .NETCoreApp behavior is compatible with its .NETStandard behavior.-->
<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Security.Permissions\src\System.Security.Permissions.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Configuration;

namespace System.Diagnostics
{
internal sealed class PerfCounterSection : ConfigurationElement
public sealed class PerfCounterSettings : ConfigurationElement
{
private static readonly ConfigurationProperty s_propFileMappingSize = new ConfigurationProperty("filemappingsize", typeof(int), 524288, ConfigurationPropertyOptions.None);
private static readonly ConfigurationPropertyCollection s_properties = new ConfigurationPropertyCollection { s_propFileMappingSize };

[ConfigurationProperty("filemappingsize", DefaultValue = 524288)]
public int FileMappingSize => (int)this[s_propFileMappingSize];

protected override ConfigurationPropertyCollection Properties => s_properties;
protected internal override ConfigurationPropertyCollection Properties => s_properties;
}
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Configuration;

namespace System.Diagnostics
{
internal sealed class SystemDiagnosticsSection : ConfigurationSection
public sealed partial class SystemDiagnosticsSection : ConfigurationSection
{
private static readonly ConfigurationPropertyCollection s_properties = new();
private static readonly ConfigurationProperty s_propSources = new("sources", typeof(SourceElementsCollection), new SourceElementsCollection(), ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty s_propSharedListeners = new("sharedListeners", typeof(SharedListenerElementsCollection), new SharedListenerElementsCollection(), ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty s_propSwitches = new("switches", typeof(SwitchElementsCollection), new SwitchElementsCollection(), ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty s_propTrace = new("trace", typeof(TraceSection), new TraceSection(), ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty s_propPerfCounters = new ConfigurationProperty("performanceCounters", typeof(PerfCounterSettings), new PerfCounterSettings(), ConfigurationPropertyOptions.None);

static SystemDiagnosticsSection()
{
s_properties.Add(s_propSources);
s_properties.Add(s_propSharedListeners);
s_properties.Add(s_propSwitches);
s_properties.Add(s_propTrace);
s_properties.Add(s_propPerfCounters);

#if NET7_0_OR_GREATER
SystemDiagnosticsSectionNetCoreApp();
#endif
}

protected internal override ConfigurationPropertyCollection Properties => s_properties;

[ConfigurationProperty("sources")]
public SourceElementsCollection Sources => (SourceElementsCollection)base[s_propSources];

[ConfigurationProperty("sharedListeners")]
public ListenerElementsCollection SharedListeners => (ListenerElementsCollection)base[s_propSharedListeners];

[ConfigurationProperty("switches")]
public SwitchElementsCollection Switches => (SwitchElementsCollection)base[s_propSwitches];

[ConfigurationProperty("trace")]
public TraceSection Trace => (TraceSection)base[s_propTrace];

protected internal override void InitializeDefault()
{
Trace.Listeners?.InitializeDefaultInternal();
}
[ConfigurationProperty("performanceCounters")]
public PerfCounterSettings PerfCounterSettings => (PerfCounterSettings)base[s_propPerfCounters];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Configuration;

namespace System.Diagnostics
{
public partial class SystemDiagnosticsSection
{
private static readonly ConfigurationProperty s_propSources = new("sources", typeof(SourceElementsCollection), new SourceElementsCollection(), ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty s_propSharedListeners = new("sharedListeners", typeof(SharedListenerElementsCollection), new SharedListenerElementsCollection(), ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty s_propSwitches = new("switches", typeof(SwitchElementsCollection), new SwitchElementsCollection(), ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty s_propTrace = new("trace", typeof(TraceSection), new TraceSection(), ConfigurationPropertyOptions.None);

private static void SystemDiagnosticsSectionNetCoreApp()
{
s_properties.Add(s_propSources);
s_properties.Add(s_propSharedListeners);
s_properties.Add(s_propSwitches);
s_properties.Add(s_propTrace);
}

[ConfigurationProperty("sources")]
internal SourceElementsCollection Sources => (SourceElementsCollection)base[s_propSources];

[ConfigurationProperty("sharedListeners")]
internal ListenerElementsCollection SharedListeners => (ListenerElementsCollection)base[s_propSharedListeners];

[ConfigurationProperty("switches")]
internal SwitchElementsCollection Switches => (SwitchElementsCollection)base[s_propSwitches];

[ConfigurationProperty("trace")]
internal TraceSection Trace => (TraceSection)base[s_propTrace];

protected internal override void InitializeDefault()
{
Trace.Listeners?.InitializeDefaultInternal();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ private static void ConfigureTraceSettings()
{
// Ported from https://referencesource.microsoft.com/#System/compmod/system/diagnostics/TraceInternal.cs,06360b4de5e221c2, https://referencesource.microsoft.com/#System/compmod/system/diagnostics/TraceInternal.cs,37

TraceSection traceSection = DiagnosticsConfiguration.SystemDiagnosticsSection?.Trace;
TraceSection traceSettings = DiagnosticsConfiguration.SystemDiagnosticsSection?.Trace;

if (traceSection != null)
if (traceSettings != null)
{
Trace.UseGlobalLock = traceSection.UseGlobalLock;
Trace.AutoFlush = traceSection.AutoFlush;
Trace.IndentSize = traceSection.IndentSize;
Trace.UseGlobalLock = traceSettings.UseGlobalLock;
Trace.AutoFlush = traceSettings.AutoFlush;
Trace.IndentSize = traceSettings.IndentSize;

ListenerElementsCollection listeners = DiagnosticsConfiguration.SystemDiagnosticsSection?.Trace.Listeners;
if (listeners != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down Expand Up @@ -29,7 +29,6 @@ System.Diagnostics.PerformanceCounter</PackageDescription>
<Compile Include="System\Diagnostics\InstanceData.cs" />
<Compile Include="System\Diagnostics\InstanceDataCollection.cs" />
<Compile Include="System\Diagnostics\InstanceDataCollectionCollection.cs" />
<Compile Include="System\Diagnostics\PerfCounterSection.cs" />
<Compile Include="System\Diagnostics\PerformanceCounter.cs">
<SubType>Component</SubType>
</Compile>
Expand All @@ -41,7 +40,6 @@ System.Diagnostics.PerformanceCounter</PackageDescription>
<Compile Include="System\Diagnostics\PerformanceCounterType.cs" />
<Compile Include="System\Diagnostics\PerformanceDataRegistryKey.cs" />
<Compile Include="System\Diagnostics\SharedPerformanceCounter.cs" />
<Compile Include="System\Diagnostics\SystemDiagnosticsSection.cs" />
<Compile Include="System\Diagnostics\TraceInternal.cs" />
<Compile Include="misc\EnvironmentHelpers.cs" />
<Compile Include="System\Diagnostics\PerformanceData\CounterSet.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ internal static int PerformanceCountersFileMappingSize

Initialize();
SystemDiagnosticsSection configSectionSav = s_configSection;
if (configSectionSav != null && configSectionSav.PerfCounters != null)
if (configSectionSav != null && configSectionSav.PerfCounterSettings != null)
{
int size = configSectionSav.PerfCounters.FileMappingSize;
int size = configSectionSav.PerfCounterSettings.FileMappingSize;
if (size < SharedPerformanceCounter.MinCountersFileMappingSize)
size = SharedPerformanceCounter.MinCountersFileMappingSize;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Diagnostics.TraceSourceConfigTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Configuration.ConfigurationManager\src\System.Configuration.ConfigurationManager.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Configuration.ConfigurationManager\src\System.Configuration.ConfigurationManager.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using Xunit;

namespace System.Diagnostics.TraceSourceConfigTests
{
// Note that parallelization is disabled due to file access as each test replaces the single config file on disk.
public class ConfigurationTests
{
private static volatile string? _configFile = null;

private static string ConfigFile
{
get
{
if (_configFile == null)
{
Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
_configFile = Path.GetFileName(config.FilePath);
}

return _configFile;
}
}

private static void CreateAndLoadConfigFile(string filename)
{
Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string dir = Path.GetDirectoryName(config.FilePath);
string from = Path.Combine(dir, filename);
File.Copy(from, ConfigFile, overwrite: true);
TraceConfiguration.Register();
Trace.Refresh();

if (_configFile == null)
{
_configFile = Path.GetFileName(config.FilePath);
File.Copy(from, _configFile, overwrite: true);
TraceConfiguration.Register();
// Do not call Trace.Refresh() here since the first access should be tested without it.
}
else
{
File.Copy(from, _configFile, overwrite: true);
Trace.Refresh();
}
}

[Fact]
Expand Down Expand Up @@ -195,7 +188,7 @@ public void Refresh_RemoveSource()
Assert.Equal(string.Empty, listenerToBeRemoved.Output);

listenerToBeRemoved.Clear();
//mySourceToBeRemoved.Close();
mySourceToBeRemoved.Close();
}

[Fact]
Expand Down Expand Up @@ -314,7 +307,18 @@ public void Switch_MissingValue_Throws()
{
Exception e = Assert.Throws<ConfigurationErrorsException>(() =>
CreateAndLoadConfigFile("testhost_Switch_MissingValue_Throws.config"));

Assert.Contains("'value'", e.ToString());
}

private sealed class PerfCounterSection : ConfigurationElement
{
private static readonly ConfigurationProperty s_propFileMappingSize = new ConfigurationProperty("filemappingsize", typeof(int), 524288, ConfigurationPropertyOptions.None);
private static readonly ConfigurationPropertyCollection s_properties = new ConfigurationPropertyCollection { s_propFileMappingSize };

[ConfigurationProperty("filemappingsize", DefaultValue = 524288)]
public int FileMappingSize => (int)this[s_propFileMappingSize];
protected override ConfigurationPropertyCollection Properties => s_properties;
}
}
}

0 comments on commit a5ac25d

Please sign in to comment.