public string ToDisplayString() =>
String.Format("{0, -40}", Name) + String.Format("0x{0, -18}", $"{Keywords:X16}") + String.Format("{0, -8}", EventLevel.ToString() + $"({(int)EventLevel})");
+
+ public static bool operator ==(Provider left, Provider right)
+ {
+ return left.Name == right.Name &&
+ left.Keywords == right.Keywords &&
+ left.EventLevel == right.EventLevel &&
+ left.FilterData == right.FilterData;
+ }
+
+ public static bool operator !=(Provider left, Provider right)
+ {
+ return !(left == right);
+ }
+
+ public override bool Equals(object obj)
+ {
+
+ if (obj == null || GetType() != obj.GetType())
+ {
+ return false;
+ }
+
+ return this == (Provider)obj;
+ }
+
+ public override int GetHashCode()
+ {
+ int hash = 0;
+ hash ^= this.Name.GetHashCode();
+ hash ^= this.Keywords.GetHashCode();
+ hash ^= this.EventLevel.GetHashCode();
+ hash ^= this.FilterData.GetHashCode();
+ return hash;
+ }
}
}
if (profile.Length != 0)
{
- var profileProviders = new List<Provider>();
var selectedProfile = ListProfilesCommandHandler.DotNETRuntimeProfiles
.FirstOrDefault(p => p.Name.Equals(profile, StringComparison.OrdinalIgnoreCase));
if (selectedProfile == null)
return ErrorCodes.ArgumentError;
}
- // If user defined a different key/level on the same provider via --providers option that was specified via --profile option,
- // --providers option takes precedence. Go through the list of providers specified and only add it if it wasn't specified
- // via --providers options.
- if (selectedProfile.Providers != null)
- {
- foreach (Provider selectedProfileProvider in selectedProfile.Providers)
- {
- bool shouldAdd = true;
-
- foreach (Provider providerCollectionProvider in providerCollection)
- {
- if (providerCollectionProvider.Name.Equals(selectedProfileProvider.Name))
- {
- shouldAdd = false;
- break;
- }
- }
-
- if (shouldAdd)
- {
- enabledBy[selectedProfileProvider.Name] = "--profile ";
- profileProviders.Add(selectedProfileProvider);
- }
- }
- }
- providerCollection.AddRange(profileProviders);
+ Profile.MergeProfileAndProviders(selectedProfile, providerCollection, enabledBy);
}
public IEnumerable<Provider> Providers { get; }
public string Description { get; }
+
+ public static void MergeProfileAndProviders(Profile selectedProfile, List<Provider> providerCollection, Dictionary<string, string> enabledBy)
+ {
+ var profileProviders = new List<Provider>();
+ // If user defined a different key/level on the same provider via --providers option that was specified via --profile option,
+ // --providers option takes precedence. Go through the list of providers specified and only add it if it wasn't specified
+ // via --providers options.
+ if (selectedProfile.Providers != null)
+ {
+ foreach (Provider selectedProfileProvider in selectedProfile.Providers)
+ {
+ bool shouldAdd = true;
+
+ foreach (Provider providerCollectionProvider in providerCollection)
+ {
+ if (providerCollectionProvider.Name.Equals(selectedProfileProvider.Name))
+ {
+ shouldAdd = false;
+ break;
+ }
+ }
+
+ if (shouldAdd)
+ {
+ enabledBy[selectedProfileProvider.Name] = "--profile ";
+ profileProviders.Add(selectedProfileProvider);
+ }
+ }
+ }
+ providerCollection.AddRange(profileProviders);
+ }
}
}
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using Xunit;
+using Microsoft.Diagnostics.Tools.RuntimeClient;
+using System.Collections.Generic;
+using System.Linq;
+using System.Diagnostics.Tracing;
+
+namespace Microsoft.Diagnostics.Tools.Trace
+{
+ public class ProfileProviderMergeTests
+ {
+ [Theory]
+ [InlineData("cpu-sampling", "Microsoft-Windows-DotNETRuntime")]
+ [InlineData("gc-verbose", "Microsoft-Windows-DotNETRuntime")]
+ [InlineData("gc-collect", "Microsoft-Windows-DotNETRuntime")]
+ public void DuplicateProvider_CorrectlyOverrides(string profileName, string providerToParse)
+ {
+ Dictionary<string, string> enabledBy = new Dictionary<string, string>();
+
+ List<Provider> parsedProviders = Extensions.ToProviders(providerToParse);
+
+ foreach (var provider in parsedProviders)
+ {
+ enabledBy[provider.Name] = "--providers";
+ }
+
+ var selectedProfile = ListProfilesCommandHandler.DotNETRuntimeProfiles
+ .FirstOrDefault(p => p.Name.Equals(profileName, StringComparison.OrdinalIgnoreCase));
+ Assert.NotNull(selectedProfile);
+
+ Profile.MergeProfileAndProviders(selectedProfile, parsedProviders, enabledBy);
+
+ var enabledProvider = parsedProviders.SingleOrDefault(p => p.Name == "Microsoft-Windows-DotNETRuntime");
+ Assert.True(enabledProvider != default(Provider));
+
+ // Assert that our specified provider overrides the version in the profile
+ Assert.True(enabledProvider.Keywords == UInt64.MaxValue);
+ Assert.True(enabledProvider.EventLevel == EventLevel.Verbose);
+ Assert.True(enabledBy[enabledProvider.Name] == "--providers");
+ }
+ }
+}
\ No newline at end of file