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.
Add log level conditioned 'ForContext'
This method creates a logger with provided property if the log level is enabled
- Loading branch information
1 parent
5bcf133
commit 2988d20
Showing
2 changed files
with
95 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,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); | ||
} | ||
} | ||
} |
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,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)); | ||
} | ||
} | ||
} |