forked from serilog/serilog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request serilog#1034 from pardahlman/feature/for-context
Add log level conditioned 'ForContext'
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |