Skip to content

Commit

Permalink
Feat: Add additional primitive values as tags on SentryLogger (#1246)
Browse files Browse the repository at this point in the history
* Add primitive values as tags.
  • Loading branch information
lucas-zimerman committed Oct 8, 2021
1 parent 6d9f660 commit 6f14500
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
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());
}
}
}
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

0 comments on commit 6f14500

Please sign in to comment.