public void Log_InScopeWithNamedProperties_LogsWithAllScopeProperties() { var logger = loggerProvider.CreateLogger(null); using (logger.BeginScope(new Dictionary <string, object> { ["key"] = "value" })) using (logger.BeginScope(new Dictionary <string, object> { ["key2"] = "value2" })) logger.LogInformation("message"); var expectedLogEvent = new VostokLogEvent(LogLevel.Info, DateTimeOffset.UtcNow, "message") .WithProperty( "operationContext", new OperationContextValue( new[] { "System.Collections.Generic.Dictionary`2[System.String,System.Object]", "System.Collections.Generic.Dictionary`2[System.String,System.Object]" })) .WithProperty("key", "value") .WithProperty("key2", "value2"); log.Events.Single() .Should() .BeEquivalentTo( expectedLogEvent, o => o.Excluding(x => x.Timestamp)); }
public void Log_InIgnoredScope_LogsWithoutScope() { loggerProvider = new VostokLoggerProvider( log, new VostokLoggerProviderSettings { IgnoredScopes = new HashSet <string> { typeof(HashSet <int>).FullName } }); var logger = loggerProvider.CreateLogger(null); using (var scope1 = logger.BeginScope(new HashSet <int>())) using (var scope2 = logger.BeginScope(new HashSet <string>())) { scope1.Should().BeOfType <EmptyDisposable>(); scope2.Should().NotBeOfType <EmptyDisposable>(); logger.LogInformation("message"); } var expectedLogEvent = new VostokLogEvent(LogLevel.Info, DateTimeOffset.UtcNow, "message") .WithProperty("operationContext", new OperationContextValue(new[] { "System.Collections.Generic.HashSet`1[System.String]" })); log.Events.Single() .Should() .BeEquivalentTo( expectedLogEvent, o => o.Excluding(x => x.Timestamp)); }
public void Log_InScopeInDifferentAsyncLocalContexts_LogsInValidScopes() { var logger = loggerProvider.CreateLogger(null); var start = new ManualResetEventSlim(false); var wait = new ManualResetEventSlim(false); var task = Task.Run( () => { using (logger.BeginScope("inner-scope")) { start.Set(); wait.Wait(); } }); start.Wait(); using (logger.BeginScope("outer-scope")) logger.LogInformation("message"); wait.Set(); task.Wait(); var expectedLogEvent = new VostokLogEvent(LogLevel.Info, DateTimeOffset.UtcNow, "message") .WithProperty("operationContext", new OperationContextValue("outer-scope")); log.Events.Single() .Should() .BeEquivalentTo( expectedLogEvent, o => o.Excluding(x => x.Timestamp)); }
public void Log_InIgnoredScopePrefix_LogsWithoutScope() { loggerProvider = new VostokLoggerProvider( log, new VostokLoggerProviderSettings { IgnoredScopePrefixes = new HashSet <string> { "system" } }); var logger = loggerProvider.CreateLogger(null); using (var scope1 = logger.BeginScope(new HashSet <int>())) using (var scope2 = logger.BeginScope(new HashSet <string>())) { scope1.Should().BeOfType <EmptyDisposable>(); scope2.Should().BeOfType <EmptyDisposable>(); logger.LogInformation("message"); } var expectedLogEvent = new VostokLogEvent(LogLevel.Info, DateTimeOffset.UtcNow, "message"); log.Events.Single() .Should() .BeEquivalentTo( expectedLogEvent, o => o.Excluding(x => x.Timestamp)); }
public void Log_method_should_keep_original_timestamp() { var @event = new LogEvent(LogLevel.Info, DateTimeOffset.Now - 5.Hours(), null); adapter.Log(@event); observedEvent.Timestamp.Should().Be(@event.Timestamp); }
public void Log_SimpleMessage_LogsValidEvent() { var expectedLogEvent = new VostokLogEvent(LogLevel.Debug, DateTimeOffset.Now, "message"); microsoftLog.Log(expectedLogEvent); oracleLog.Events.Single().Should().BeSameAs(expectedLogEvent); }
public void Log_method_should_correctly_convert_levels(LogLevel vostokLevel, SerilogLevel serilogLevel) { var @event = new LogEvent(vostokLevel, DateTimeOffset.Now, null); adapter.Log(@event); observedEvent.Level.Should().Be(serilogLevel); }
public void Log_method_should_keep_original_exception() { var @event = new LogEvent(LogLevel.Info, DateTimeOffset.Now, null, new Exception("I failed.")); adapter.Log(@event); observedEvent.Exception.Should().BeSameAs(@event.Exception); }
public void Log_WithException_LogsValidEvent() { var exception = new Exception("exception"); var expectedLogEvent = new VostokLogEvent(LogLevel.Info, DateTimeOffset.Now, "message", exception); microsoftLog.Log(expectedLogEvent); oracleLog.Events.Single().Should().BeSameAs(expectedLogEvent); }
private MessageTemplate CreateTemplate(LogEvent @event) { if (logger.BindMessageTemplate(@event.MessageTemplate, Array.Empty <object>(), out var serilogTemplate, out _)) { return(serilogTemplate); } return(MessageTemplate.Empty); }
private SerilogEvent TranslateEvent(LogEvent @event) { return(new SerilogEvent( @event.Timestamp, TranslateLevel(@event.Level), @event.Exception, CreateTemplate(@event), CreateProperties(@event))); }
public void Log_MessageWithNamedPlaceholders_LogsValidEvent() { var expectedLogEvent = new VostokLogEvent(LogLevel.Debug, DateTimeOffset.Now, "message {p1} {p2}") .WithProperty("p1", "v1") .WithProperty("p2", "v2"); microsoftLog.Log(expectedLogEvent); oracleLog.Events.Single().Should().BeSameAs(expectedLogEvent); }
public void Log_MessageWithPositionPlaceholders_LogsValidEvent() { var expectedLogEvent = new VostokLogEvent(LogLevel.Fatal, DateTimeOffset.Now, "message {0} {1}") .WithProperty("0", "v1") .WithProperty("1", "v2"); microsoftLog.Log(expectedLogEvent); oracleLog.Events.Single().Should().BeSameAs(expectedLogEvent); }
public void Log_method_should_translate_all_properties_not_present_in_template() { var @event = new LogEvent(LogLevel.Info, DateTimeOffset.Now, null) .WithProperty("Param1", 1) .WithProperty("Param2", 2); adapter.Log(@event); observedEvent.Properties["Param1"].Should().BeOfType <ScalarValue>().Which.Value.Should().Be(1); observedEvent.Properties["Param2"].Should().BeOfType <ScalarValue>().Which.Value.Should().Be(2); }
public void Log_SimpleMessage_LogsValidEvent() { loggerProvider.CreateLogger(null).LogInformation("message"); var expectedLogEvent = new VostokLogEvent(LogLevel.Info, DateTimeOffset.Now, "message"); log.Events.Single() .Should() .BeEquivalentTo( expectedLogEvent, o => o.Excluding(x => x.Timestamp)); }
/// <inheritdoc /> public void Log(LogEvent @event) { if (@event == null) { return; } if (IsEnabledFor(@event.Level)) { logger.Write(TranslateEvent(@event)); } }
public void Log_MessageWithPositionPlaceholders_LogsValidEvent() { loggerProvider.CreateLogger(null).LogCritical("message {0} {1}", "v1", "v2"); var expectedLogEvent = new VostokLogEvent(LogLevel.Fatal, DateTimeOffset.Now, "message {0} {1}") .WithProperty("0", "v1") .WithProperty("1", "v2"); log.Events.Single() .Should() .BeEquivalentTo( expectedLogEvent, o => o.Excluding(x => x.Timestamp)); }
public void Log_WithComplexMicrosoftLoggerScope_LogsValidEvent() { var expectedLogEvent = new VostokLogEvent(LogLevel.Info, DateTimeOffset.Now, "message") .WithProperty("operationContext", new OperationContextValue("{ id = 1, name = 2 }")); using (microsoftLogger.BeginScope(new { id = 1, name = 2 })) { microsoftLog.Info("message"); oracleLog.Events.Single() .Should() .BeEquivalentTo(expectedLogEvent, o => o.Excluding(x => x.Timestamp)); } }
public void Log_MessageWithNamedPlaceholders_LogsValidEvent() { loggerProvider.CreateLogger(null).LogDebug("message {p1} {p2}", "v1", "v2"); var expectedLogEvent = new VostokLogEvent(LogLevel.Debug, DateTimeOffset.Now, "message {p1} {p2}") .WithProperty("p1", "v1") .WithProperty("p2", "v2"); log.Events.Single() .Should() .BeEquivalentTo( expectedLogEvent, o => o.Excluding(x => x.Timestamp)); }
private IEnumerable <LogEventProperty> CreateProperties(LogEvent @event) { if (@event.Properties == null) { yield break; } foreach (var pair in @event.Properties) { if (logger.BindProperty(pair.Key, pair.Value, false, out var serilogProperty)) { yield return(serilogProperty); } } }
public void Log_WithSimpleMicrosoftLoggerScopeAndException_LogsValidEvent() { const string scope = "s1"; var exception = new Exception("exception"); var expectedLogEvent = new VostokLogEvent(LogLevel.Info, DateTimeOffset.Now, "message", exception) .WithProperty("operationContext", new OperationContextValue(scope)); using (microsoftLogger.BeginScope(scope)) { microsoftLog.Info(exception, "message"); oracleLog.Events.Single() .Should() .BeEquivalentTo(expectedLogEvent, o => o.Excluding(x => x.Timestamp)); } }
public void Log(LogEvent logEvent) { if (logEvent == null) { return; } if (logEvent.Exception is ApiRequestException) { return; } var sourceContext = LoggerSetup.GetSourceContext(logEvent); var renderedEvent = LogEventFormatter.Format(logEvent, OutputTemplate).EscapeMarkdown(); var message = $"{logEvent.Level} log message from *{sourceContext}*:\n\n```{renderedEvent}```"; errorsBot.PostToChannel(message, ParseMode.Markdown); }
public void Log_WithEventIdWithNAMEOnly_LogsValidEvent() { loggerProvider = new VostokLoggerProvider(log, new VostokLoggerProviderSettings { AddEventIdProperties = true }); loggerProvider.CreateLogger(null).LogCritical(new EventId(0, "eventId"), "message"); var expectedLogEvent = new VostokLogEvent(LogLevel.Fatal, DateTimeOffset.Now, "message") .WithProperty("EventId.Name", "eventId"); log.Events.Single() .Should() .BeEquivalentTo( expectedLogEvent, o => o.Excluding(x => x.Timestamp)); }
public void Log_AfterScopeWithProperties_LogsWithoutScopeProperties() { var logger = loggerProvider.CreateLogger(null); using (logger.BeginScope("scope {sp1} {sp2}", "sv1", "sv2")) { logger.LogInformation("message {p1} {p2}", "v1", "v2"); } logger.LogInformation("message {p1} {p2}", "v1", "v2"); var expectedLogEvent = new VostokLogEvent(LogLevel.Info, DateTimeOffset.UtcNow, "message {p1} {p2}") .WithProperty("p1", "v1") .WithProperty("p2", "v2"); log.Events.Last() .Should() .BeEquivalentTo( expectedLogEvent, o => o.Excluding(x => x.Timestamp)); }
public void Log_InNestedScope_LogsWithNestedScope() { var logger = loggerProvider.CreateLogger(null); using (logger.BeginScope("s1")) using (logger.BeginScope("s2")) { logger.LogInformation("message {p1} {p2}", "v1", "v2"); } var expectedLogEvent = new VostokLogEvent(LogLevel.Info, DateTimeOffset.UtcNow, "message {p1} {p2}") .WithProperty("operationContext", new OperationContextValue(new[] { "s1", "s2" })) .WithProperty("p1", "v1") .WithProperty("p2", "v2"); log.Events.Single() .Should() .BeEquivalentTo( expectedLogEvent, o => o.Excluding(x => x.Timestamp)); }
public static LogEvent WithObjectProperties <T>(this LogEvent @event, T @object, bool allowOverwrite = true, bool allowNullValues = true) => @event.MutateProperties(eventProperties => eventProperties.WithObjectProperties(@object, allowOverwrite, allowNullValues));