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

tags should not differ based on current culture #1949

Merged
merged 12 commits into from
Sep 26, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- Exception filters should consider child exceptions of an `AggregateException` ([#1924](https://github.com/getsentry/sentry-dotnet/pull/1924))
- Add Blazor WASM detection to set IsGlobalModeEnabled to true ([#1931](https://github.com/getsentry/sentry-dotnet/pull/1931))
- Respect Transaction.IsSampled in SqlListener ([#1933](https://github.com/getsentry/sentry-dotnet/pull/1933))
- Tags should not differ based on current culture ([#1949](https://github.com/getsentry/sentry-dotnet/pull/1949))

## 3.21.0

Expand Down
114 changes: 61 additions & 53 deletions src/Sentry.Extensions.Logging/SentryLogger.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using Microsoft.Extensions.Logging;
using Sentry.Infrastructure;

Expand Down Expand Up @@ -47,59 +48,7 @@ public void Log<TState>(

if (ShouldCaptureEvent(logLevel, eventId, exception))
{
var @event = new SentryEvent(exception)
{
Logger = CategoryName,
Message = message,
Level = logLevel.ToSentryLevel()
};

if (state is IEnumerable<KeyValuePair<string, object?>> pairs)
{
foreach (var property in pairs)
{
if (property.Key == "{OriginalFormat}" && property.Value is string template)
{
// Original format found, use Sentry logEntry interface
@event.Message = new SentryMessage
{
Formatted = message,
Message = template
};
continue;
}

switch (property.Value)
{
case string stringTagValue:
@event.SetTag(property.Key, stringTagValue);
break;

case Guid guidTagValue when guidTagValue != Guid.Empty:
@event.SetTag(property.Key, guidTagValue.ToString());
break;

case Enum enumValue:
@event.SetTag(property.Key, enumValue.ToString());
break;

default:
{
if (property.Value?.GetType().IsPrimitive == true)
{
@event.SetTag(property.Key, property.Value.ToString()!);
}
break;
}
}
}
}

var tuple = eventId.ToTupleOrNull();
if (tuple.HasValue)
{
@event.SetTag(tuple.Value.name, tuple.Value.value);
}
var @event = CreateEvent(logLevel, eventId, state, exception, message, CategoryName);

_ = _hub.CaptureEvent(@event);
}
Expand Down Expand Up @@ -127,6 +76,65 @@ public void Log<TState>(
}
}

internal static SentryEvent CreateEvent<TState>(LogLevel logLevel, EventId id, TState state, Exception? exception, string? message, string category)
{
var @event = new SentryEvent(exception)
{
Logger = category,
Message = message,
Level = logLevel.ToSentryLevel()
};

if (state is IEnumerable<KeyValuePair<string, object>> pairs)
{
foreach (var property in pairs)
{
if (property.Key == "{OriginalFormat}" && property.Value is string template)
{
// Original format found, use Sentry logEntry interface
@event.Message = new SentryMessage
{
Formatted = message,
Message = template
};
continue;
}

switch (property.Value)
{
case string stringTagValue:
@event.SetTag(property.Key, stringTagValue);
break;

case Guid guidTagValue when guidTagValue != Guid.Empty:
@event.SetTag(property.Key, guidTagValue.ToString());
break;

case Enum enumValue:
@event.SetTag(property.Key, enumValue.ToString());
break;

default:
{
if (property.Value?.GetType().IsPrimitive == true)
{
@event.SetTag(property.Key, Convert.ToString(property.Value, CultureInfo.InvariantCulture)!);
}
break;
}
}
}
}

var tuple = id.ToTupleOrNull();
if (tuple.HasValue)
{
@event.SetTag(tuple.Value.name, tuple.Value.value);
}

return @event;
}

private bool ShouldCaptureEvent(
LogLevel logLevel,
EventId eventId,
Expand Down
28 changes: 28 additions & 0 deletions test/Sentry.Extensions.Logging.Tests/SentryLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,34 @@ public void Log_WithProperties_SetsTagsInEvent()
e.Tags["fooEnum"] == "Absolute"));
}

[Fact]
public void Culture_does_not_effect_tags()
{
var props = new List<KeyValuePair<string, object>>
{
new("fooInteger", 12345),
new("fooDouble", 12345.123d),
new("fooFloat", 1234.123f),
};
SentryEvent sentryEvent;
var culture = Thread.CurrentThread.CurrentCulture;

try
{
Thread.CurrentThread.CurrentCulture = new("da-DK");
sentryEvent = SentryLogger.CreateEvent(LogLevel.Debug, default, props, null, null, "category");
}
finally
{
Thread.CurrentThread.CurrentCulture = culture;
}

var tags = sentryEvent.Tags;
Assert.Equal("12345", tags["fooInteger"]);
Assert.Equal("12345.123", tags["fooDouble"]);
Assert.Equal("1234.123", tags["fooFloat"]);
}

[Fact]
public void Log_WithEmptyGuidProperty_DoesntSetTagInEvent()
{
Expand Down