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

Added truncate ambient property, e.g. ${message:truncate=80} #3328

Merged
merged 1 commit into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace NLog.LayoutRenderers.Wrappers
/// Left part of a text
/// </summary>
[LayoutRenderer("left")]
[AmbientProperty("Truncate")]
[AppDomainFixedOutput]
[ThreadAgnostic]
[ThreadSafe]
Expand All @@ -53,6 +54,14 @@ public sealed class LeftLayoutRendererWrapper : WrapperLayoutRendererBase
[RequiredParameter]
public int Length { get; set; }

/// <summary>
/// Same as <see cref="Length"/>-property, so it can be used as ambient property.
/// </summary>
/// <example>
/// ${message:truncate=80}
/// </example>
public int Truncate { get => Length; set => Length = value; }

/// <inheritdoc/>
protected override void RenderInnerAndTransform(LogEventInfo logEvent, StringBuilder builder, int orgLength)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ namespace NLog.UnitTests.LayoutRenderers.Wrappers
public class LeftLayoutRendererWrapperTests
{
[Theory]
[InlineData(":length=2", "12")]
[InlineData(":length=1000", "1234567890")]
[InlineData(":length=-1", "")]
[InlineData(":length=0", "")]
public void LeftWrapperTest(string options, string expected)
[InlineData(2, "12")]
[InlineData(1000, "1234567890")]
[InlineData(-1, "")]
[InlineData(0, "")]
public void LeftWrapperTest(int length, string expected)
{
SimpleLayout l = $"${{left:${{message}}{options}}}";
SimpleLayout l = $"${{left:${{message}}:length={length}}}";
var result = l.Render(LogEventInfo.Create(LogLevel.Debug, "substringTest", "1234567890"));
Assert.Equal(expected, result);

l = $"${{message:truncate={length}}}";
result = l.Render(LogEventInfo.Create(LogLevel.Debug, "substringTest", "1234567890"));
Assert.Equal(expected, result);
}
}
}