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

Fixed a string truncate bug #506

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -202,5 +202,28 @@ public void GetAdditionalColumnNameAndValueReturnsTruncatedForCharacterTypesWith
Assert.Equal(columnName, result.Key);
Assert.Equal("Additio...", result.Value);
}

[Fact]
Whinarn marked this conversation as resolved.
Show resolved Hide resolved
[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);
}
}
}