Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add additional primitive values as tags on SentryLogger #1246

Merged
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Add additional primitive values as tags on SentryLogger ([#1246](https://github.com/getsentry/sentry-dotnet/pull/1246))

## 3.9.4

### Fixes
Expand Down
21 changes: 19 additions & 2 deletions src/Sentry.Extensions.Logging/SentryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,26 @@ public void Log<TState>(
continue;
}

if (property.Value is string tagValue)
if (property.Value is string stringTagValue)
{
@event.SetTag(property.Key, tagValue);
@event.SetTag(property.Key, stringTagValue);
}
else if (property.Value is int integerTagValue)
{
@event.SetTag(property.Key, integerTagValue.ToString());
}
else if (property.Value is float floatTagValue)
{
@event.SetTag(property.Key, floatTagValue.ToString());
}
else if (property.Value is double doubleTagValue)
{
@event.SetTag(property.Key, doubleTagValue.ToString());
}
else if (property.Value is Guid guidTagValue &&
guidTagValue != Guid.Empty)
{
@event.SetTag(property.Key, guidTagValue.ToString());
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
30 changes: 28 additions & 2 deletions test/Sentry.Extensions.Logging.Tests/SentryLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,44 @@ public void Log_WithEventId_EventIdAsTagOnEvent()

[Fact]
public void Log_WithProperties_SetsTagsInEvent()
{
var guidValue = Guid.NewGuid();

var props = new List<KeyValuePair<string, object>>
{
new("fooString", "bar"),
new("fooInteger", 1234),
new("fooDouble", (double)1234),
new("fooFloat", (float)1234.123),
new("fooGuid", guidValue)
};
var sut = _fixture.GetSut();

sut.Log<object>(LogLevel.Critical, default, props, null, null);

_ = _fixture.Hub.Received(1)
.CaptureEvent(Arg.Is<SentryEvent>(
e => e.Tags["fooString"] == "bar" &&
e.Tags["fooInteger"] == "1234" &&
e.Tags["fooDouble"] == "1234" &&
e.Tags["fooFloat"] == "1234.123" &&
e.Tags["fooGuid"] == guidValue.ToString()));
}

[Fact]
public void Log_WithEmptyGuidProperty_DoesntSetTagInEvent()
{
var props = new List<KeyValuePair<string, object>>
{
new("foo", "bar")
new("fooGuid", Guid.Empty)
};
var sut = _fixture.GetSut();

sut.Log<object>(LogLevel.Critical, default, props, null, null);

_ = _fixture.Hub.Received(1)
.CaptureEvent(Arg.Is<SentryEvent>(
e => e.Tags["foo"] == "bar"));
e => e.Tags.Count == 0));
}

[Fact]
Expand Down