Skip to content

Commit

Permalink
Log4JXmlEventLayout - Added support for configuration of Parameters (…
Browse files Browse the repository at this point in the history
…Unit Test)
  • Loading branch information
snakefoot committed May 11, 2019
1 parent 714c242 commit c05cd95
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/NLog/LayoutRenderers/Log4JXmlEventLayoutRenderer.cs
Expand Up @@ -385,11 +385,13 @@ private void AppendNdc(XmlWriter xtw)

private void AppendParameters(LogEventInfo logEvent, XmlWriter xtw)
{
for (int i = 0; i < Parameters.Count; ++i)
for (int i = 0; i < Parameters?.Count; ++i)
{
var parameter = Parameters[i];
if (string.IsNullOrEmpty(parameter?.Name))
continue;

var parameterValue = parameter.Layout.Render(logEvent);
var parameterValue = parameter.Layout?.Render(logEvent) ?? string.Empty;
if (!parameter.IncludeEmptyValue && string.IsNullOrEmpty(parameterValue))
continue;

Expand Down
2 changes: 1 addition & 1 deletion src/NLog/Layouts/Log4JXmlEventLayout.cs
Expand Up @@ -72,7 +72,7 @@ public Log4JXmlEventLayout()
/// </summary>
/// <docgen category='Payload Options' order='10' />
[ArrayParameter(typeof(NLogViewerParameterInfo), "parameter")]
public IList<NLogViewerParameterInfo> Parameters { get; }
public IList<NLogViewerParameterInfo> Parameters { get => Renderer.Parameters; set => Renderer.Parameters = value; }

/// <summary>
/// Gets or sets a value indicating whether to include contents of the <see cref="MappedDiagnosticsContext"/> dictionary.
Expand Down
50 changes: 40 additions & 10 deletions tests/NLog.UnitTests/LayoutRenderers/Log4JXmlTests.cs
Expand Up @@ -31,19 +31,20 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//

using System.Collections.Generic;
using NLog.Config;

namespace NLog.UnitTests.LayoutRenderers
{
using System.Threading;
using System.Diagnostics;
using System;
using System.Xml;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Xunit;
using System.Reflection;
using System.Threading;
using System.Xml;
using NLog.Config;
using NLog.Internal;
using NLog.Layouts;
using NLog.Targets;
using Xunit;

public class Log4JXmlTests : NLogTestBase
{
Expand All @@ -53,8 +54,8 @@ public void Log4JXmlTest()
LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
<nlog throwExceptions='true'>
<targets>
<target name='debug' type='Debug' layout='${log4jxmlevent:includeCallSite=true:includeSourceInfo=true:includeNdlc=true:includeMdc=true:IncludeNdc=true:includeMdlc=true:IncludeAllProperties=true:ndcItemSeparator=\:\::includenlogdata=true:loggerName=${logger}}' />
</targets>
<target name='debug' type='Debug' layout='${log4jxmlevent:includeCallSite=true:includeSourceInfo=true:includeNdlc=true:includeMdc=true:IncludeNdc=true:includeMdlc=true:IncludeAllProperties=true:ndcItemSeparator=\:\::includenlogdata=true:loggerName=${logger}}' />
</targets>
<rules>
<logger name='*' minlevel='Debug' writeTo='debug' />
</rules>
Expand Down Expand Up @@ -235,6 +236,35 @@ public void Log4JXmlTest()
}
}

[Fact]
void Log4JXmlEventLayoutParameterTest()
{
var log4jLayout = new Log4JXmlEventLayout()
{
Parameters =
{
new NLogViewerParameterInfo
{
Name = "mt",
Layout = "${message:raw=true}",
}
},
};
log4jLayout.Renderer.AppInfo = "MyApp";
var logEventInfo = new LogEventInfo
{
LoggerName = "MyLOgger",
TimeStamp = new DateTime(2010, 01, 01, 12, 34, 56, DateTimeKind.Utc),
Level = LogLevel.Info,
Message = "hello, {0}",
Parameters = new[] { "world" },
};

var threadid = Environment.CurrentManagedThreadId;
var machinename = Environment.MachineName;
Assert.Equal($"<log4j:event logger=\"MyLOgger\" level=\"INFO\" timestamp=\"1262349296000\" thread=\"{threadid}\"><log4j:message>hello, world</log4j:message><log4j:properties><log4j:data name=\"mt\" value=\"hello, {{0}}\" /><log4j:data name=\"log4japp\" value=\"MyApp\" /><log4j:data name=\"log4jmachinename\" value=\"{machinename}\" /></log4j:properties></log4j:event>", log4jLayout.Render(logEventInfo));
}

[Fact]
void BadXmlValueTest()
{
Expand Down

0 comments on commit c05cd95

Please sign in to comment.