public void Log_messages_have_SourceContext_set_to_SerilogLoggerFacade_FullName() { LogEvent logEvent = null; var logger = DelegatingSink.GetLogger(le => logEvent = le); var target = new SerilogLoggerFacade(logger); target.Log(string.Empty, Category.Exception, Priority.None); Assert.True(logEvent.Properties.ContainsKey("SourceContext")); var sourceContext = logEvent.Properties["SourceContext"].LiteralValue(); Assert.Equal(typeof(SerilogLoggerFacade).FullName, sourceContext); }
public void DictionariesAreSerialisedAsObjects() { var dict = new DictionaryValue(new[] { new KeyValuePair<ScalarValue, LogEventPropertyValue>( new ScalarValue(1), new ScalarValue("hello")), new KeyValuePair<ScalarValue, LogEventPropertyValue>( new ScalarValue("world"), new SequenceValue(new [] { new ScalarValue(1.2) })) }); var e = DelegatingSink.GetLogEvent(l => l.Information("Value is {ADictionary}", dict)); var f = FormatJson(e); Assert.AreEqual("hello", (string)f.Properties.ADictionary["1"]); Assert.AreEqual(1.2, (double)f.Properties.ADictionary.world[0]); }
private static void TestPriorityToPropertyMap(Priority priority, string propertyName, string expectedValue) { LogEvent logEvent = null; var logger = DelegatingSink.GetLogger(le => logEvent = le); var target = new SerilogLoggerFacade(logger); target.Log(string.Empty, Category.Exception, priority); Assert.True(logEvent.Properties.ContainsKey(propertyName)); var priorityValue = logEvent.Properties[propertyName].LiteralValue(); Assert.Equal(expectedValue, priorityValue); }
public void AnEmptyPropertiesTokenIsAnEmptyStructureValue() { var formatter = new OutputTemplateRenderer(DefaultThemes.None, "{Properties}", null); var evt = DelegatingSink.GetLogEvent(l => l.Information("Hello")); var sw = new StringWriter(); formatter.Format(evt, sw); // /!\ different behavior from Serilog Core : https://github.com/serilog/serilog/blob/5c3a7821aa0f654e551dc21e8e19089f6767666b/test/Serilog.Tests/Formatting/Display/MessageTemplateTextFormatterTests.cs#L268-L278 // // var expected = new StructureValue(Enumerable.Empty<LogEventProperty>()).ToString(); // // expected == "{ }" // Assert.Equal(expected, sw.ToString()); // Assert.Equal("{}", sw.ToString()); }
public void WriteToLoggerWithConfigCallbackMinimumLevelOverrideInheritanceScenarios( string rootOverrideSource, int rootOverrideLevelIncrement, string childOverrideSource, int childOverrideLevelIncrement, bool eventShouldGetToChild) { var incomingEventLevel = Information; var rootOverrideLevel = incomingEventLevel + rootOverrideLevelIncrement; var childOverrideLevel = incomingEventLevel + childOverrideLevelIncrement; LogEvent evt = null; var sink = new DelegatingSink(e => evt = e); var rootLoggerConfig = new LoggerConfiguration() .MinimumLevel.Is(LevelAlias.Minimum); if (rootOverrideSource != null) { rootLoggerConfig.MinimumLevel.Override(rootOverrideSource, rootOverrideLevel); } var logger = rootLoggerConfig .WriteTo.Logger(lc => { lc.MinimumLevel.Is(LevelAlias.Minimum); if (childOverrideSource != null) { lc.MinimumLevel.Override(childOverrideSource, childOverrideLevel); } lc.WriteTo.Sink(sink); }) .CreateLogger(); logger .ForContext(Constants.SourceContextPropertyName, "Root.N1.N2") .Write(Some.LogEvent(level: incomingEventLevel)); if (eventShouldGetToChild) { Assert.NotNull(evt); } else { Assert.Null(evt); } }
public void SpecifyingThatATypeIsScalarCausesItToBeLoggedAsScalarEvenWhenDestructuring() { var events = new List <LogEvent>(); var sink = new DelegatingSink(events.Add); var logger = new LoggerConfiguration() .WriteTo.Sink(sink) .Destructure.AsScalar(typeof(AB)) .CreateLogger(); logger.Information("{@AB}", new AB()); var ev = events.Single(); var prop = ev.Properties["AB"]; Assert.IsType <ScalarValue>(prop); }
public void AFilterPreventsMatchedEventsFromPassingToTheSink() { var excluded = Some.InformationEvent(); var included = Some.InformationEvent(); var filter = new DelegateFilter(e => e.MessageTemplate != excluded.MessageTemplate); var events = new List <LogEvent>(); var sink = new DelegatingSink(events.Add); var logger = new LoggerConfiguration() .WriteTo.Sink(sink) .Filter.With(filter) .CreateLogger(); logger.Write(included); logger.Write(excluded); Assert.Single(events); Assert.Contains(included, events); }
public void DestructuringSystemTypeGivesScalarByDefault() { var events = new List <LogEvent>(); var sink = new DelegatingSink(events.Add); var logger = new LoggerConfiguration() .WriteTo.Sink(sink) .CreateLogger(); var thisType = GetType(); logger.Information("{@thisType}", thisType); var ev = events.Single(); var prop = ev.Properties["thisType"]; var sv = Assert.IsAssignableFrom <ScalarValue>(prop); Assert.Equal(thisType, sv.LiteralValue()); }
public void TransformationsAreAppliedToEventProperties() { var events = new List <LogEvent>(); var sink = new DelegatingSink(events.Add); var logger = new LoggerConfiguration() .WriteTo.Sink(sink) .Destructure.ByTransforming <AB>(ab => new { C = ab.B }) .CreateLogger(); logger.Information("{@AB}", new AB()); var ev = events.Single(); var prop = ev.Properties["AB"]; var sv = (StructureValue)prop; var c = sv.Properties.Single(); Assert.AreEqual("C", c.Name); }
public void WriteToLoggerMinimumLevelOverrideInheritanceNotSupportedScenarios( string rootOverrideSource, int rootOverrideLevelIncrement, string childOverrideSource, int childOverrideLevelIncrement) { var incomingEventLevel = Information; var rootOverrideLevel = incomingEventLevel + rootOverrideLevelIncrement; var childOverrideLevel = incomingEventLevel + childOverrideLevelIncrement; LogEvent evt = null; var sink = new DelegatingSink(e => evt = e); var childLoggerConfig = new LoggerConfiguration() .MinimumLevel.Is(LevelAlias.Minimum); if (childOverrideSource != null) { childLoggerConfig.MinimumLevel.Override(childOverrideSource, childOverrideLevel); } childLoggerConfig.WriteTo.Sink(sink); var childLogger = childLoggerConfig.CreateLogger(); var rootLoggerConfig = new LoggerConfiguration() .MinimumLevel.Is(LevelAlias.Minimum); if (rootOverrideSource != null) { rootLoggerConfig.MinimumLevel.Override(rootOverrideSource, rootOverrideLevel); } var logger = rootLoggerConfig .WriteTo.Logger(childLogger) .CreateLogger(); logger .ForContext(Constants.SourceContextPropertyName, "Root.N1.N2") .Write(Some.LogEvent(level: incomingEventLevel)); // even though the use may expect no event Assert.NotNull(evt); }
public void DestructuringIsPossibleForSystemTypeDerivedProperties() { var events = new List <LogEvent>(); var sink = new DelegatingSink(events.Add); var logger = new LoggerConfiguration() .Destructure.With(new ProjectedDestructuringPolicy( canApply: t => typeof(Type).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()), projection: o => ((Type)o).AssemblyQualifiedName)) .WriteTo.Sink(sink) .CreateLogger(); var thisType = GetType(); logger.Information("{@thisType}", thisType); var ev = events.Single(); var prop = ev.Properties["thisType"]; var sv = Assert.IsAssignableFrom <ScalarValue>(prop); Assert.Equal(thisType.AssemblyQualifiedName, sv.LiteralValue()); }
public void FormatProviderWithScalarProperties(string format, bool shouldUseCustomFormatter) { var frenchFormatProvider = new CultureInfo("fr-FR"); var defaultFormatProvider = CultureInfo.InvariantCulture; var date = new DateTime(2018, 01, 01); var number = 12.345; var expectedFormattedDate = shouldUseCustomFormatter ? date.ToString(frenchFormatProvider) : date.ToString("O", defaultFormatProvider); var expectedFormattedNumber = shouldUseCustomFormatter ? number.ToString(frenchFormatProvider) : number.ToString(defaultFormatProvider); var formatter = new OutputTemplateRenderer(DefaultThemes.None, "{Message" + format + "}", frenchFormatProvider); var evt = DelegatingSink.GetLogEvent(l => { l.Information("{MyDate}{MyNumber}", date, number); }); var sw = new StringWriter(); formatter.Format(evt, sw); Assert.Contains(expectedFormattedDate, sw.ToString()); Assert.Contains(expectedFormattedNumber, sw.ToString()); }
public void ParametersForAnEmptyTemplateAreIgnored() { var e = DelegatingSink.GetLogEvent(l => l.Error("message", new object())); Assert.Equal("message", e.RenderMessage()); }