Skip to content

Commit

Permalink
LeftLayoutRendererWrapper - Added truncate as ambient property (#3328)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored and 304NotModified committed Apr 22, 2019
1 parent 34d23d5 commit 3c16e69
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
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);
}
}
}

0 comments on commit 3c16e69

Please sign in to comment.