Skip to content

Commit 287b9d3

Browse files
authored
Merge pull request #506 from Whinarn/bug/505-dont-truncate-short-strings
Fixed a string truncate bug
2 parents 215e91b + 1769dac commit 287b9d3

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/Serilog.Sinks.MSSqlServer/Extensions/StringExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ public static string TruncateOutput(this string value, int dataLength) =>
1212
public static string Truncate(this string value, int maxLength, string suffix)
1313
{
1414
if (value == null) return null;
15+
else if (value.Length <= maxLength) return value;
16+
1517
var suffixLength = suffix?.Length ?? 0;
1618
if (maxLength <= suffixLength) return string.Empty;
1719

1820
var correctedMaxLength = maxLength - suffixLength;
19-
return value.Length <= maxLength ? value : Invariant($"{value.Substring(0, correctedMaxLength)}{suffix}");
21+
return Invariant($"{value.Substring(0, correctedMaxLength)}{suffix}");
2022
}
2123
}
2224
}

test/Serilog.Sinks.MSSqlServer.Tests/Extensions/StringExtensionsTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ public void ReturnTruncatedStringWithSuffix()
6262
Assert.Equal("A simple tes...", truncatedMessage);
6363
}
6464

65+
[Theory]
66+
[InlineData("Abc")]
67+
[InlineData("Ab")]
68+
[InlineData("X")]
69+
[Trait("Bugfix", "#505")]
70+
public void ReturnNonTruncatedShortStringWhenMaxLengthIsLessOrEqualToSuffixLength(string inputMessage)
71+
{
72+
// Act
73+
var nonTruncatedMessage = inputMessage.Truncate(3, "...");
74+
75+
// Assert
76+
Assert.Equal(inputMessage, nonTruncatedMessage);
77+
}
78+
6579
[Fact]
6680
public void ReturnTruncatedStringWithEmptySuffix()
6781
{

test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Output/AdditionalColumnDataGeneratorTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,28 @@ public void GetAdditionalColumnNameAndValueReturnsTruncatedForCharacterTypesWith
202202
Assert.Equal(columnName, result.Key);
203203
Assert.Equal("Additio...", result.Value);
204204
}
205+
206+
[Fact]
207+
[Trait("Bugfix", "#505")]
208+
public void GetAdditionalColumnNameAndValueReturnsFullStringWithOneDataLength()
209+
{
210+
// Arrange
211+
const string columnName = "AdditionalProperty1";
212+
const string propertyValue = "A";
213+
var additionalColumn = new SqlColumn(columnName, SqlDbType.NVarChar);
214+
additionalColumn.DataLength = 1;
215+
var properties = new Dictionary<string, LogEventPropertyValue>();
216+
_columnSimplePropertyValueResolver.Setup(r => r.GetPropertyValueForColumn(
217+
It.IsAny<SqlColumn>(), It.IsAny<IReadOnlyDictionary<string, LogEventPropertyValue>>()))
218+
.Returns(new KeyValuePair<string, LogEventPropertyValue>(columnName, new ScalarValue(propertyValue)));
219+
220+
// Act
221+
var result = _sut.GetAdditionalColumnNameAndValue(additionalColumn, properties);
222+
223+
// Assert
224+
_columnSimplePropertyValueResolver.Verify(r => r.GetPropertyValueForColumn(additionalColumn, properties), Times.Once);
225+
Assert.Equal(columnName, result.Key);
226+
Assert.Equal(propertyValue, result.Value);
227+
}
205228
}
206229
}

0 commit comments

Comments
 (0)