Skip to content

Commit

Permalink
Merge pull request #47 from serilog/dev
Browse files Browse the repository at this point in the history
5.1.1 Release
  • Loading branch information
QuantumNightmare authored Mar 3, 2022
2 parents 477c4ed + b699c24 commit 598824c
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 13 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions serilog-sinks-raygun.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/Serilog.Sinks.Raygun/Serilog.Sinks.Raygun.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageTags>serilog sink raygun</PackageTags>
<Copyright>Copyright © Serilog Contributors 2017-2020</Copyright>
<Description>Serilog event sink that writes to the Raygun service.</Description>
<VersionPrefix>5.1.0</VersionPrefix>
<VersionPrefix>5.1.1</VersionPrefix>
<RootNamespace>Serilog</RootNamespace>
</PropertyGroup>

Expand All @@ -27,8 +27,7 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="mindscape.Raygun4Net.AspNetCore" Version="6.6.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.1.2" />
<PackageReference Include="mindscape.Raygun4Net.AspNetCore" Version="6.6.5" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,31 @@ 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)
{
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);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Serilog.Sinks.Raygun/Sinks/Raygun/RaygunSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -204,15 +204,15 @@ 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);
}

// 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);
}
Expand Down
20 changes: 20 additions & 0 deletions test/Serilog.Sinks.Raygun.Tests/Serilog.Sinks.Raygun.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="NUnit" Version="3.13.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Serilog.Sinks.Raygun\Serilog.Sinks.Raygun.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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<LogEventPropertyValue>()));
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<LogEventPropertyValue>()));
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<ScalarValue, LogEventPropertyValue>(new ScalarValue("item1"), new ScalarValue("item1_value")),
new KeyValuePair<ScalarValue, LogEventPropertyValue>(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<LogEventPropertyValue>()));
IDictionary outputValue = logEventProperty.AsDictionary();

Assert.That(outputValue, Is.EqualTo(null));
}
}
}

0 comments on commit 598824c

Please sign in to comment.