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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support {Properties} in output templates #944

Merged
merged 8 commits into from Apr 5, 2017
@@ -0,0 +1,68 @@
// Copyright 2017 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.IO;
using Serilog.Events;

namespace Serilog.Formatting.Display
{
class LogEventPropertiesValue : LogEventPropertyValue
{
readonly MessageTemplate _template;
readonly IReadOnlyDictionary<string, LogEventPropertyValue> _properties;

public LogEventPropertiesValue(MessageTemplate template, IReadOnlyDictionary<string, LogEventPropertyValue> properties)
{
_template = template;
_properties = properties;
}

public override void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null)
{
output.Write('{');

var delim = "";
foreach (var kvp in _properties)
{
if (TemplateContainsPropertyName(_template, kvp.Key))
{
continue;
}

output.Write(delim);
delim = ", ";
output.Write(kvp.Key);
output.Write(": ");
kvp.Value.Render(output, null, formatProvider);
}

output.Write('}');
}

static bool TemplateContainsPropertyName(MessageTemplate template, string propertyName)
{
foreach (var namedProperty in template.NamedProperties)

This comment has been minimized.

Copy link
@nblumhardt

nblumhardt Mar 10, 2017

Member

@Pliner I actually had the ol' for(var i = ... in mind here.

{
if (namedProperty.PropertyName == propertyName)
{
return true;
}
}

return false;
}
}
}
@@ -50,6 +50,11 @@ public static class OutputProperties
/// </summary>
public const string ExceptionPropertyName = "Exception";

/// <summary>
/// The properties of the log event.
/// </summary>
public const string PropertiesPropertyName = "Properties";

/// <summary>
/// Create properties from the provided log event.
/// </summary>
@@ -67,11 +72,12 @@ public static class OutputProperties
result[TimestampPropertyName] = new ScalarValue(logEvent.Timestamp);
result[LevelPropertyName] = new LogEventLevelValue(logEvent.Level);
result[NewLinePropertyName] = new LiteralStringValue(Environment.NewLine);
result[PropertiesPropertyName] = new LogEventPropertiesValue(logEvent.MessageTemplate, logEvent.Properties);

var exception = logEvent.Exception == null ? "" : (logEvent.Exception + Environment.NewLine);
var exception = logEvent.Exception == null ? "" : logEvent.Exception + Environment.NewLine;
result[ExceptionPropertyName] = new LiteralStringValue(exception);

return result;
}
}
}
}
@@ -198,5 +198,15 @@ public void AppliesCustomFormatterToEnums()
formatter.Format(evt, sw);
Assert.Equal("Size Huge", sw.ToString());
}

[Fact]
public void NonMessagePropertiesAreRendered()
{
var formatter = new MessageTemplateTextFormatter("{Properties}", CultureInfo.InvariantCulture);
var evt = DelegatingSink.GetLogEvent(l => l.ForContext("Foo", 42).Information("Hello from {Bar}!", "bar"));
var sw = new StringWriter();
formatter.Format(evt, sw);
Assert.Equal("{Foo: 42}", sw.ToString());
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can鈥檛 perform that action at this time.