Skip to content

Commit

Permalink
Bugfix: Setting Component and Operation via indexer
Browse files Browse the repository at this point in the history
I've encountered a few codebases where Component and Operation are customized using eventContext["(Component|Operation)"] rather than the first-class properties

Generally, this is fine as the customized values end up in the rendered log message, however, it can result in unexpected behavior when the getters don't reflect the change.  For example, if an application framework suppresses log events based on Component/Operation
  • Loading branch information
chris-peterson committed Jan 3, 2024
1 parent 056a4a3 commit 9fade3c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
11 changes: 5 additions & 6 deletions src/Spiffy.Monitoring/EventContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,18 @@ public EventContext() : this(null, null)

public double ElapsedMilliseconds => _timer.ElapsedMilliseconds;

string _component;
public string Component
{
get => _component;
set => this["Component"] = _component = value;
get => this["Component"] as string;
set => this["Component"] = value;
}

string _operation;
public string Operation
{
get => _operation;
set => this["Operation"] = _operation = value;
get => this["Operation"] as string;
set => this["Operation"] = value;
}

public Level Level { get; private set; }

readonly ConcurrentDictionary<string, (uint Order, object Value)> _values =
Expand Down
4 changes: 2 additions & 2 deletions src/Spiffy.Monitoring/Spiffy.Monitoring.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Copyright>2014-2023</Copyright>
<Copyright>2014-2024</Copyright>
<Authors>Chris Peterson</Authors>
<Description>A monitoring framework for .NET that supports IoC and modern targets, e.g. Splunk</Description>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -13,7 +13,7 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>6.3.0</Version>
<Version>6.3.1</Version>
<RootNamespace>Spiffy.Monitoring</RootNamespace>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Expand Down
69 changes: 69 additions & 0 deletions tests/UnitTests/EventContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,73 @@ void time_is_now()
Context.Render().Timestamp.Should().BeWithin(TimeSpan.FromMinutes(1));
}
}

public class SetComponent : Scenarios<EventContext>
{
[Scenario]
public void UsingProperty()
{
When(Setting_via_property);
Then(Value_is_reflected);
}

[Scenario]
public void UsingIndexer()
{
When(Setting_via_indexer);
Then(Value_is_reflected);
}

const string CUSTOM_VALUE = "foobar123";
void Setting_via_property()
{
Context.Component = CUSTOM_VALUE;
}

void Setting_via_indexer()
{
Context["Component"] = CUSTOM_VALUE;
}


void Value_is_reflected()
{
Context.Component.Should().Be(CUSTOM_VALUE);
Context.Render().Message.Should().Contain(CUSTOM_VALUE);
}
}

public class SetOperation : Scenarios<EventContext>
{
[Scenario]
public void UsingProperty()
{
When(Setting_via_property);
Then(Value_is_reflected);
}

[Scenario]
public void UsingIndexer()
{
When(Setting_via_indexer);
Then(Value_is_reflected);
}

const string CUSTOM_VALUE = "foobar123";
void Setting_via_property()
{
Context.Operation = CUSTOM_VALUE;
}

void Setting_via_indexer()
{
Context["Operation"] = CUSTOM_VALUE;
}

void Value_is_reflected()
{
Context.Operation.Should().Be(CUSTOM_VALUE);
Context.Render().Message.Should().Contain(CUSTOM_VALUE);
}
}
}

0 comments on commit 9fade3c

Please sign in to comment.