Skip to content

Commit

Permalink
Merge pull request #514 from serilog-mssql/dev
Browse files Browse the repository at this point in the history
Release 6.5.1 (2nd attempt)
  • Loading branch information
ckadluba authored Jan 29, 2024
2 parents 7311915 + 2d38ec3 commit b1d8553
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 6.5.1
* Fixed issue #505: NVarChar Columns with 1 data length gets their values truncated into empty strings (thanks to @Whinarn)
* Updated Microsoft.Data.SqlClient to 5.1.4 to fix CVE-2024-0056 (https://github.com/advisories/GHSA-98g6-xh36-x2p7)

# 6.5.0
* Implemented #488: Support OpenTelemetry TraceId and SpanId as provided by Serilog core
* Include README in NuGet package
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageVersion Include="System.Runtime.Extensions" Version="4.3.1" />
<PackageVersion Include="System.Runtime.InteropServices" Version="4.3.0" />
<PackageVersion Include="System.Text.Encoding.Extensions" Version="4.3.0" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.0.1" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.1.4" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
Expand Down
4 changes: 3 additions & 1 deletion src/Serilog.Sinks.MSSqlServer/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ public static string TruncateOutput(this string value, int dataLength) =>
public static string Truncate(this string value, int maxLength, string suffix)
{
if (value == null) return null;
else if (value.Length <= maxLength) return value;

var suffixLength = suffix?.Length ?? 0;
if (maxLength <= suffixLength) return string.Empty;

var correctedMaxLength = maxLength - suffixLength;
return value.Length <= maxLength ? value : Invariant($"{value.Substring(0, correctedMaxLength)}{suffix}");
return Invariant($"{value.Substring(0, correctedMaxLength)}{suffix}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>A Serilog sink that writes events to Microsoft SQL Server and Azure SQL</Description>
<VersionPrefix>6.5.0</VersionPrefix>
<VersionPrefix>6.5.1</VersionPrefix>
<Authors>Michiel van Oudheusden;Christian Kadluba;Serilog Contributors</Authors>
<TargetFrameworks>netstandard2.0;net462;net472;net6.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ public void ReturnTruncatedStringWithSuffix()
Assert.Equal("A simple tes...", truncatedMessage);
}

[Theory]
[InlineData("Abc")]
[InlineData("Ab")]
[InlineData("X")]
[Trait("Bugfix", "#505")]
public void ReturnNonTruncatedShortStringWhenMaxLengthIsLessOrEqualToSuffixLength(string inputMessage)
{
// Act
var nonTruncatedMessage = inputMessage.Truncate(3, "...");

// Assert
Assert.Equal(inputMessage, nonTruncatedMessage);
}

[Fact]
public void ReturnTruncatedStringWithEmptySuffix()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,28 @@ public void GetAdditionalColumnNameAndValueReturnsTruncatedForCharacterTypesWith
Assert.Equal(columnName, result.Key);
Assert.Equal("Additio...", result.Value);
}

[Fact]
[Trait("Bugfix", "#505")]
public void GetAdditionalColumnNameAndValueReturnsFullStringWithOneDataLength()
{
// Arrange
const string columnName = "AdditionalProperty1";
const string propertyValue = "A";
var additionalColumn = new SqlColumn(columnName, SqlDbType.NVarChar);
additionalColumn.DataLength = 1;
var properties = new Dictionary<string, LogEventPropertyValue>();
_columnSimplePropertyValueResolver.Setup(r => r.GetPropertyValueForColumn(
It.IsAny<SqlColumn>(), It.IsAny<IReadOnlyDictionary<string, LogEventPropertyValue>>()))
.Returns(new KeyValuePair<string, LogEventPropertyValue>(columnName, new ScalarValue(propertyValue)));

// Act
var result = _sut.GetAdditionalColumnNameAndValue(additionalColumn, properties);

// Assert
_columnSimplePropertyValueResolver.Verify(r => r.GetPropertyValueForColumn(additionalColumn, properties), Times.Once);
Assert.Equal(columnName, result.Key);
Assert.Equal(propertyValue, result.Value);
}
}
}

0 comments on commit b1d8553

Please sign in to comment.