Skip to content

Commit

Permalink
Add log level conditioned 'ForContext'
Browse files Browse the repository at this point in the history
This method creates a logger with provided property
if the log level is enabled
  • Loading branch information
pardahlman committed Sep 29, 2017
1 parent 5bcf133 commit 2988d20
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Serilog/Core/ForContextExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Serilog.Events;

namespace Serilog
{
/// <summary>
/// Extension method 'ForContext' for ILogger.
/// </summary>
public static class ForContextExtension
{
/// <summary>
/// Create a logger that enriches log events with the specified property based on log event level.
/// </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)
{
return !logger.IsEnabled(level)
? logger
: logger.ForContext(propertyName, value, destructureObjects);
}
}
}
61 changes: 61 additions & 0 deletions test/Serilog.Tests/Core/ForContextExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using Serilog.Events;
using Serilog.Tests.Support;
using Xunit;

namespace Serilog.Tests.Core
{
public class ForContextExtensionsTests
{
[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)
{
// Arrange
var propValue = Guid.NewGuid();
var propKey = Some.String();
var sink = new CollectingSink();
var logger = new LoggerConfiguration()
.MinimumLevel.Is(logMinLevel)
.WriteTo.Sink(sink)
.CreateLogger();

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

// Assert
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)
{
// Arrange
var propValue = Guid.NewGuid();
var propKey = Some.String();
var sink = new CollectingSink();
var logger = new LoggerConfiguration()
.MinimumLevel.Is(logMinLevel)
.WriteTo.Sink(sink)
.CreateLogger();

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

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

0 comments on commit 2988d20

Please sign in to comment.