/// <summary> /// Write a log event with the error level. /// </summary> /// <param name="template">Message template describing the event.</param> /// <param name="properties">Objects positionally formatted into the message template.</param> public void Error(string template, params object[] properties) { using (LogContext.PushProperties(new LogEventEnricher(properties))) { _logger.Error(template, properties); } }
public void MultipleNestedPropertiesOverrideLessNestedOnes() { LogEvent lastEvent = null; var log = new LoggerConfiguration() .Enrich.FromLogContext() .WriteTo.Sink(new DelegatingSink(e => lastEvent = e)) .CreateLogger(); using (LogContext.PushProperties(new PropertyEnricher("A1", 1), new PropertyEnricher("A2", 2))) { log.Write(Some.InformationEvent()); Assert.Equal(1, lastEvent.Properties["A1"].LiteralValue()); Assert.Equal(2, lastEvent.Properties["A2"].LiteralValue()); using (LogContext.PushProperties(new PropertyEnricher("A1", 10), new PropertyEnricher("A2", 20))) { log.Write(Some.InformationEvent()); Assert.Equal(10, lastEvent.Properties["A1"].LiteralValue()); Assert.Equal(20, lastEvent.Properties["A2"].LiteralValue()); } log.Write(Some.InformationEvent()); Assert.Equal(1, lastEvent.Properties["A1"].LiteralValue()); Assert.Equal(2, lastEvent.Properties["A2"].LiteralValue()); } log.Write(Some.InformationEvent()); Assert.False(lastEvent.Properties.ContainsKey("A1")); Assert.False(lastEvent.Properties.ContainsKey("A2")); }
/// <summary> /// Write a log event with the debug level and associated exception. /// </summary> /// <param name="exception">Exception related to the event.</param> /// <param name="template">Message template describing the event.</param> /// <param name="properties">Objects positionally formatted into the message template.</param> public void Debug(Exception exception, string template, params object[] properties) { using (LogContext.PushProperties(new LogEventEnricher(properties))) { _logger.Debug(exception, template, properties); } }
public void OnActionExecuting(ActionExecutingContext context) { if (context.ModelState.IsValid) { return; } // Normally I use Elasticsearch to handle error logging. When that // sink is setup using the LogContext allows me to record some extra // information about the current request. using (LogContext.PushProperties(BuildIdentityEnrichers(context.HttpContext.User))) { _logger.Warning("Model validation failed for {@Input} with validation {@Errors}", context.ActionArguments, context.ModelState? .SelectMany(kvp => kvp.Value.Errors) .Select(e => e.ErrorMessage)); } // I split validation errors into two categories: Input and Domain. // - Input: High level validation like ensuring required fields are // not null and that certain data fields are correctly // formatted (e.g. email address or phone number) // - Domain: Validation errors that depend on the state of the mode. // For example, the current user doesn't have permission to // post reviews. // // In both cases a 400 response is sent back to the client. The // "Type" field is used to indicate where the error happened. context.Result = new BadRequestObjectResult( from kvp in context.ModelState from e in kvp.Value.Errors let k = kvp.Key select new ValidationError(ValidationError.Type.Input, null, k, e.ErrorMessage)); }
public async Task Invoke(HttpContext httpContext) { var properties = GetProperties(httpContext).ToArray(); using (LogContext.PushProperties(properties)) { await next.Invoke(httpContext); } }
public SerilogLoggerScope(SerilogLoggerProvider provider, object state) { _provider = provider; _state = state; Parent = _provider.CurrentScope; _provider.CurrentScope = this; _popSerilogContext = LogContext.PushProperties(this); }
public void Execute(TContext context, Action <TContext> next) { var properties = GetProperties(context).ToArray(); using (LogContext.PushProperties(properties)) { next(context); } }
/// <inheritdoc /> public IDisposable BeginScope <T>(T state) { if (CurrentScope != null) { return(new SerilogLoggerScope(this, state)); } // The outermost scope pushes and pops the Serilog `LogContext` - once // this enricher is on the stack, the `CurrentScope` property takes care // of the rest of the `BeginScope()` stack. var popSerilogContext = LogContext.PushProperties(this); return(new SerilogLoggerScope(this, state, popSerilogContext)); }
public void SharedBeforeEach() { TestContext.CurrentContext.Test.Properties.Set("TestCaseId", Guid.NewGuid().ToString("N")); _logContext = LogContext.PushProperties(new PropertyEnricher("TestCaseId", TestCaseId), new PropertyEnricher("TestName", TestContext.CurrentContext.Test.FullName)); Log.Information("Test Start|{TestName}", TestContext.CurrentContext.Test.FullName); }