public GetEffectiveLevel ( string context, LogEventLevel &minimumLevel, |
||
context | string | |
minimumLevel | LogEventLevel | |
levelSwitch | ||
return | void |
/// <summary> /// Create a logger that enriches log events with the specified property. /// </summary> /// <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> public Logger ForContext(string propertyName, object value, bool destructureObjects = false) { if (!LogEventProperty.IsValidName(propertyName)) { SelfLog.WriteLine("Attempt to call ForContext() with invalid property name `{0}` (value: `{1}`)", propertyName, value); return(this); } // It'd be nice to do the destructuring lazily, but unfortunately `value` may be mutated between // now and the first log event written... // A future optimization opportunity may be to implement ILogEventEnricher on LogEventProperty to // remove one more allocation. var enricher = new FixedPropertyEnricher(_messageTemplateProcessor.CreateProperty(propertyName, value, destructureObjects)); var levelSwitch = _levelSwitch; if (_overrideMap != null && propertyName == Constants.SourceContextPropertyName) { var context = value as string; if (context != null) { _overrideMap.GetEffectiveLevel(context, out levelSwitch); } } return(new Logger( this, enricher, null, levelSwitch, _overrideMap)); }
public void OverrideScenarios(string context, bool overrideExpected, LogEventLevel expected) { var overrides = new Dictionary<string, LoggingLevelSwitch> { ["MyApp"] = new LoggingLevelSwitch(LogEventLevel.Debug), ["MyApp.Api.Controllers"] = new LoggingLevelSwitch(LogEventLevel.Information), ["MyApp.Api.Controllers.HomeController"] = new LoggingLevelSwitch(LogEventLevel.Warning), ["MyApp.Api"] = new LoggingLevelSwitch(LogEventLevel.Error) }; var lom = new LevelOverrideMap(overrides, LogEventLevel.Fatal, null); LoggingLevelSwitch overriddenSwitch; LogEventLevel overriddenLevel; lom.GetEffectiveLevel(context, out overriddenLevel, out overriddenSwitch); if (overrideExpected) { Assert.NotNull(overriddenSwitch); Assert.Equal(expected, overriddenSwitch.MinimumLevel); Assert.Equal(LevelAlias.Minimum, overriddenLevel); } else { Assert.Equal(LogEventLevel.Fatal, overriddenLevel); Assert.Null(overriddenSwitch); } }