Skip to content

Commit

Permalink
Fix TraceSwitch to synchronize the Value and Level properties (#89278)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh authored Jul 26, 2023
1 parent e1ee453 commit d4d0812
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -2030,6 +2031,26 @@ public void ComplexObj_As_Enumerable_Element()
ValidateGeolocation(obj);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
public void TraceSwitchTest()
{
var dic = new Dictionary<string, string>
{
{"TraceSwitch:Level", "Info"}
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
var config = configurationBuilder.Build();

TraceSwitch ts = new(displayName: "TraceSwitch", description: "This switch is set via config.");
ConfigurationBinder.Bind(config, "TraceSwitch", ts);
Assert.Equal(TraceLevel.Info, ts.Level);
#if NETCOREAPP
// Value property is not publicly exposed in .NET Framework.
Assert.Equal("Info", ts.Value);
#endif // NETCOREAPP
}

private void ValidateGeolocation(IGeolocation location)
{
Assert.Equal(3, location.Latitude);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ protected int SwitchSetting

protected internal virtual string[]? GetSupportedAttributes() => null;

internal void SetSwitchValues(int switchSetting, string switchValueString)
{
Initialize();

Debug.Assert(switchValueString is not null, "Unexpected 'switchValueString' null value");
lock (InitializedLock)
{
_switchSetting = switchSetting;
_switchValueString = switchValueString;
}
}

/// <summary>
/// The default value assigned in the constructor.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public TraceLevel Level
{
if (value < TraceLevel.Off || value > TraceLevel.Verbose)
throw new ArgumentException(SR.TraceSwitchInvalidLevel);
SwitchSetting = (int)value;
SetSwitchValues((int)value, value.ToString());
}
}

Expand Down Expand Up @@ -93,7 +93,21 @@ protected override void OnSwitchSettingChanged()

protected override void OnValueChanged()
{
SwitchSetting = (int)Enum.Parse<TraceLevel>(Value, true);
// Parse the new value and set the switch setting accordingly. Parse should throw an exception if the value is invalid.
TraceLevel level = Enum.Parse<TraceLevel>(Value, true);

if (level < TraceLevel.Off)
{
level = TraceLevel.Off;
}
else if (level > TraceLevel.Verbose)
{
level = TraceLevel.Verbose;
}


// Update the switch value string accordingly.
SetSwitchValues((int)level, level.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,25 @@ public void SwitchSettingChangedTest()
item.SetSwitchSetting((int)TraceLevel.Verbose + 1);
Assert.Equal(TraceLevel.Verbose, item.Level);
}

[Fact]
public void EnsureValueSynchronizedWIthLevel()
{
TraceSwitch ts = new TraceSwitch("Name", "Description");
Assert.Equal(TraceLevel.Off, ts.Level);
Assert.Equal("Off", ts.Value);

ts.Value = "1";
Assert.Equal(TraceLevel.Error, ts.Level);
Assert.Equal("Error", ts.Value);

ts.Level = TraceLevel.Warning;
Assert.Equal(TraceLevel.Warning, ts.Level);
Assert.Equal("Warning", ts.Value);

ts.Value = "Verbose";
Assert.Equal(TraceLevel.Verbose, ts.Level);
Assert.Equal("Verbose", ts.Value);
}
}
}

0 comments on commit d4d0812

Please sign in to comment.