diff --git a/appveyor.yml b/appveyor.yml index 5dddc0f..e7434dc 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,6 +1,6 @@ version: '{build}' skip_tags: true -image: Visual Studio 2017 +image: Visual Studio 2019 configuration: Release install: - ps: mkdir -Force ".\build\" | Out-Null diff --git a/serilog-sinks-raygun.sln b/serilog-sinks-raygun.sln index 0f5ea2f..3e9ddbb 100644 --- a/serilog-sinks-raygun.sln +++ b/serilog-sinks-raygun.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Sinks.Raygun", "src\Serilog.Sinks.Raygun\Serilog.Sinks.Raygun.csproj", "{B4EEC1AD-6E2B-49F6-A4C0-A9D39E9C4EFA}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Sinks.Raygun.Tests", "test\Serilog.Sinks.Raygun.Tests\Serilog.Sinks.Raygun.Tests.csproj", "{E7D3E0A9-08FE-4B4F-B7D6-56FF8053B532}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {B4EEC1AD-6E2B-49F6-A4C0-A9D39E9C4EFA}.Debug|Any CPU.Build.0 = Debug|Any CPU {B4EEC1AD-6E2B-49F6-A4C0-A9D39E9C4EFA}.Release|Any CPU.ActiveCfg = Release|Any CPU {B4EEC1AD-6E2B-49F6-A4C0-A9D39E9C4EFA}.Release|Any CPU.Build.0 = Release|Any CPU + {E7D3E0A9-08FE-4B4F-B7D6-56FF8053B532}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E7D3E0A9-08FE-4B4F-B7D6-56FF8053B532}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E7D3E0A9-08FE-4B4F-B7D6-56FF8053B532}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E7D3E0A9-08FE-4B4F-B7D6-56FF8053B532}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Serilog.Sinks.Raygun/Serilog.Sinks.Raygun.csproj b/src/Serilog.Sinks.Raygun/Serilog.Sinks.Raygun.csproj index 7ec9df6..f302b63 100644 --- a/src/Serilog.Sinks.Raygun/Serilog.Sinks.Raygun.csproj +++ b/src/Serilog.Sinks.Raygun/Serilog.Sinks.Raygun.csproj @@ -14,7 +14,7 @@ serilog sink raygun Copyright © Serilog Contributors 2017-2020 Serilog event sink that writes to the Raygun service. - 5.1.0 + 5.1.1 Serilog @@ -27,8 +27,7 @@ - - + diff --git a/src/Serilog.Sinks.Raygun/Sinks/Raygun/LogEventPropertyExtensions.cs b/src/Serilog.Sinks.Raygun/Sinks/Raygun/LogEventPropertyExtensions.cs index 177bcf0..88187c0 100644 --- a/src/Serilog.Sinks.Raygun/Sinks/Raygun/LogEventPropertyExtensions.cs +++ b/src/Serilog.Sinks.Raygun/Sinks/Raygun/LogEventPropertyExtensions.cs @@ -6,16 +6,23 @@ namespace Serilog.Sinks.Raygun { public static class LogEventPropertyExtensions { + public static string AsString(this LogEventPropertyValue propertyValue) + { + if (!(propertyValue is ScalarValue scalar)) return null; + // Handle string values differently as the ToString() method will wrap the string in unwanted quotes + return scalar.Value is string s ? s : scalar.ToString(); + } + public static string AsString(this LogEventProperty property) { - var scalar = property.Value as ScalarValue; - return scalar?.Value != null ? property.Value.ToString("l", null) : null; + return property.Value.AsString(); } public static int AsInteger(this LogEventProperty property, int defaultIfNull = 0) { var scalar = property.Value as ScalarValue; - return scalar?.Value != null ? int.TryParse(property.Value.ToString(), out int result) ? result : defaultIfNull : defaultIfNull; + if (scalar?.Value == null) return defaultIfNull; + return int.TryParse(property.Value.AsString(), out int result) ? result : defaultIfNull; } public static IDictionary AsDictionary(this LogEventProperty property) @@ -23,7 +30,7 @@ public static IDictionary AsDictionary(this LogEventProperty property) if (!(property.Value is DictionaryValue value)) return null; return value.Elements.ToDictionary( - kv => kv.Key.ToString("l", null), + kv => kv.Key.AsString(), kv => kv.Value is ScalarValue scalarValue ? scalarValue.Value : kv.Value); } } diff --git a/src/Serilog.Sinks.Raygun/Sinks/Raygun/RaygunSink.cs b/src/Serilog.Sinks.Raygun/Sinks/Raygun/RaygunSink.cs index 2b75ecb..edc9c44 100644 --- a/src/Serilog.Sinks.Raygun/Sinks/Raygun/RaygunSink.cs +++ b/src/Serilog.Sinks.Raygun/Sinks/Raygun/RaygunSink.cs @@ -152,8 +152,8 @@ private void OnCustomGroupingKey(object sender, RaygunCustomGroupingKeyEventArgs { details.Error = new RaygunErrorMessage { - ClassName = properties[LogMessageTemplateProperty].ToString("l", null), - Message = properties[RenderedLogMessageProperty].ToString("l", null), + ClassName = properties[LogMessageTemplateProperty].AsString(), + Message = properties[RenderedLogMessageProperty].AsString(), StackTrace = RaygunErrorMessageBuilder.BuildStackTrace(nullException.CodeExecutionStackTrace) }; } @@ -194,7 +194,7 @@ occurredOnPropertyValue is ScalarValue occurredOnScalar && properties.ContainsKey(_userNameProperty) && properties[_userNameProperty] != null) { - details.User = new RaygunIdentifierMessage(properties[_userNameProperty].ToString("l", null)); + details.User = new RaygunIdentifierMessage(properties[_userNameProperty].AsString()); properties.Remove(_userNameProperty); } @@ -204,7 +204,7 @@ occurredOnPropertyValue is ScalarValue occurredOnScalar && properties.ContainsKey(_applicationVersionProperty) && properties[_applicationVersionProperty] != null) { - details.Version = properties[_applicationVersionProperty].ToString("l", null); + details.Version = properties[_applicationVersionProperty].AsString(); properties.Remove(_applicationVersionProperty); } @@ -212,7 +212,7 @@ occurredOnPropertyValue is ScalarValue occurredOnScalar && // Add the custom group key if provided if (properties.TryGetValue(_groupKeyProperty, out var customKey)) { - details.GroupingKey = customKey.ToString("l", null); + details.GroupingKey = customKey.AsString(); properties.Remove(_groupKeyProperty); } diff --git a/test/Serilog.Sinks.Raygun.Tests/Serilog.Sinks.Raygun.Tests.csproj b/test/Serilog.Sinks.Raygun.Tests/Serilog.Sinks.Raygun.Tests.csproj new file mode 100644 index 0000000..62a0250 --- /dev/null +++ b/test/Serilog.Sinks.Raygun.Tests/Serilog.Sinks.Raygun.Tests.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + + diff --git a/test/Serilog.Sinks.Raygun.Tests/Sinks/Raygun/LogEventPropertyExtensionsTests.cs b/test/Serilog.Sinks.Raygun.Tests/Sinks/Raygun/LogEventPropertyExtensionsTests.cs new file mode 100644 index 0000000..97193a0 --- /dev/null +++ b/test/Serilog.Sinks.Raygun.Tests/Sinks/Raygun/LogEventPropertyExtensionsTests.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using NUnit.Framework; +using Serilog.Events; + +namespace Serilog.Sinks.Raygun.Tests.Sinks.Raygun +{ + [TestFixture] + public class LogEventPropertyExtensionsTests + { + static object[] AsString_WithScalarValue_Cases = + { + new object[] { "test-value", "test-value" }, + new object[] { Guid.Parse("{1DF0D385-220D-49F0-A53E-717E3E313E7C}"), "1df0d385-220d-49f0-a53e-717e3e313e7c" }, + new object[] { 1337, "1337" }, + new object[] { 1337.7331, "1337.7331" }, + new object[] { null, "null" } + }; + + static object[] AsInteger_WithScalarValue_Cases = + { + new object[] { "-1", -1 }, + new object[] { "1337", 1337 }, + new object[] { "invalid", 0 }, + new object[] { null, 0 } + }; + + [TestCaseSource(nameof(AsString_WithScalarValue_Cases))] + public void AsString_WithScalarValue_ReturnsExpectedString(object scalarValue, string expectedValue) + { + var logEventProperty = new LogEventProperty("test", new ScalarValue(scalarValue)); + string outputValue = logEventProperty.AsString(); + + Assert.That(outputValue, Is.EqualTo(expectedValue)); + } + + [Test] + public void AsString_WithSequenceValue_ReturnsNull() + { + var logEventProperty = new LogEventProperty("test", new SequenceValue(Array.Empty())); + string outputValue = logEventProperty.AsString(); + + Assert.That(outputValue, Is.EqualTo(null)); + } + + [TestCaseSource(nameof(AsInteger_WithScalarValue_Cases))] + public void AsInteger_WithScalarValue_ReturnsExpectedInteger(object scalarValue, int expectedValue) + { + var logEventProperty = new LogEventProperty("test", new ScalarValue(scalarValue)); + int outputValue = logEventProperty.AsInteger(); + + Assert.That(outputValue, Is.EqualTo(expectedValue)); + } + + [Test] + public void AsInteger_WithSequenceValue_ReturnsDefaultValue() + { + var logEventProperty = new LogEventProperty("test", new SequenceValue(Array.Empty())); + int outputValue = logEventProperty.AsInteger(99); + + Assert.That(outputValue, Is.EqualTo(99)); + } + + [Test] + public void AsDictionary_WithDictionaryValue_ReturnsDictionaryWithCorrectValues() + { + var logEventProperty = new LogEventProperty("test", new DictionaryValue(new[] + { + new KeyValuePair(new ScalarValue("item1"), new ScalarValue("item1_value")), + new KeyValuePair(new ScalarValue("item2"), new ScalarValue("item2_value")) + })); + + IDictionary outputValue = logEventProperty.AsDictionary(); + + Assert.That(outputValue, Contains.Key("item1").WithValue("item1_value")); + Assert.That(outputValue, Contains.Key("item2").WithValue("item2_value")); + } + + [Test] + public void AsDictionary_WithSequenceValue_ReturnsNull() + { + var logEventProperty = new LogEventProperty("test", new SequenceValue(Array.Empty())); + IDictionary outputValue = logEventProperty.AsDictionary(); + + Assert.That(outputValue, Is.EqualTo(null)); + } + } +} \ No newline at end of file