Skip to content

Commit

Permalink
Merge pull request serilog#1034 from pardahlman/feature/for-context
Browse files Browse the repository at this point in the history
Add log level conditioned 'ForContext'
  • Loading branch information
nblumhardt authored Oct 11, 2017
2 parents b33a4aa + 4073104 commit 3d8a2e7
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Serilog/Core/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using Serilog.Events;

namespace Serilog
{
/// <summary>
/// Extends <see cref="ILogger" /> with additional methods.
/// </summary>
public static class LoggerExtensions
{
/// <summary>
/// Create a logger that enriches log events when the specified level is enabled.
/// </summary>
/// <typeparam name="TValue"> The type of the property value. </typeparam>
/// <param name="logger">The logger.</param>
/// <param name="level">The log event level used to determine if log is enriched with property.</param>
/// <param name="propertyName">The name of the property. Must be non-empty.</param>
/// <param name="value">The property value.</param>
/// <param name="destructureObjects">If true, the value will be serialized as a structured
/// object if possible; if false, the object will be recorded as a scalar or simple array.</param>
/// <returns>A logger that will enrich log events as specified.</returns>
/// <returns></returns>
public static ILogger ForContext<TValue>(
this ILogger logger,
LogEventLevel level,
string propertyName,
TValue value,
bool destructureObjects = false)
{
if (logger == null)
throw new ArgumentNullException(nameof(logger));

return logger.IsEnabled(level)
? logger.ForContext(propertyName, value, destructureObjects)
: logger;
}
}
}
55 changes: 55 additions & 0 deletions test/Serilog.Tests/Core/LoggerExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using Serilog.Events;
using Serilog.Tests.Support;
using Xunit;

namespace Serilog.Tests.Core
{
public class LoggerExtensionsTests
{
[Theory]
[InlineData(LogEventLevel.Debug, LogEventLevel.Debug)]
[InlineData(LogEventLevel.Debug, LogEventLevel.Information)]
[InlineData(LogEventLevel.Debug, LogEventLevel.Error)]
[InlineData(LogEventLevel.Debug, LogEventLevel.Fatal)]
[InlineData(LogEventLevel.Debug, LogEventLevel.Warning)]
public void ShouldEnrichLogEventWhenLevelIsSameOrHigherThanMinLevel(LogEventLevel logMinLevel, LogEventLevel propertLogLevel)
{
var propValue = Guid.NewGuid();
var propKey = Some.String();
var sink = new CollectingSink();
var logger = new LoggerConfiguration()
.MinimumLevel.Is(logMinLevel)
.WriteTo.Sink(sink)
.CreateLogger();

logger.ForContext(propertLogLevel, propKey, propValue)
.Write(logMinLevel, string.Empty);

Assert.True(sink.SingleEvent.Properties.ContainsKey(propKey));
Assert.Equal(sink.SingleEvent.Properties[propKey].LiteralValue(), propValue);
}

[Theory]
[InlineData(LogEventLevel.Debug, LogEventLevel.Verbose)]
[InlineData(LogEventLevel.Information, LogEventLevel.Debug)]
[InlineData(LogEventLevel.Warning, LogEventLevel.Information)]
[InlineData(LogEventLevel.Error, LogEventLevel.Warning)]
[InlineData(LogEventLevel.Fatal, LogEventLevel.Error)]
public void ShouldNotEnrichLogEventsWhenMinLevelIsHigherThanProvidedLogLevel(LogEventLevel logMinLevel, LogEventLevel propertLogLevel)
{
var propValue = Guid.NewGuid();
var propKey = Some.String();
var sink = new CollectingSink();
var logger = new LoggerConfiguration()
.MinimumLevel.Is(logMinLevel)
.WriteTo.Sink(sink)
.CreateLogger();

logger.ForContext(propertLogLevel, propKey, propValue)
.Write(logMinLevel, string.Empty);

Assert.True(!sink.SingleEvent.Properties.ContainsKey(propKey));
}
}
}

0 comments on commit 3d8a2e7

Please sign in to comment.