Exemple #1
0
        public void ForContextAddingPropertiesTests()
        {
            var dummySink = new DummySink();
            var log       = new SerilogLogger(new LoggerConfiguration().WriteTo.Sink(dummySink).CreateLogger());

            var messageTemplate = "Testing adding {prop2} props";

            log.ForContext("prop", "value").InfoFormat(messageTemplate, "awesome");
            Log.CloseAndFlush();

            var result = dummySink.Events.SingleOrDefault();

            Assert.NotNull(result);
            Assert.AreEqual(LogEventLevel.Information, result.Level);
            Assert.AreEqual(messageTemplate, result.MessageTemplate.Text);
            Assert.True(result.Properties.ContainsKey("prop"));
            Assert.AreEqual("\"value\"", result.Properties["prop"].ToString());
            Assert.True(result.Properties.ContainsKey("prop2"));
            Assert.AreEqual("\"awesome\"", result.Properties["prop2"].ToString());
        }
Exemple #2
0
        public void PushPropertyTests()
        {
            var dummySink = new DummySink();

            LogManager.LogFactory = new SerilogFactory(new LoggerConfiguration()
                                                       .Enrich.FromLogContext()
                                                       .WriteTo.Sink(dummySink)
                                                       .CreateLogger());

            var log = LogManager.GetLogger(typeof(SerilogLoggerTests));

            using (log.PushProperty("A", "1"))
                using (log.PushProperty("B", "2"))
                {
                    log.Info("log entry");
                }

            var result = dummySink.Events[0];

            Assert.That(result.Properties.Any(x => x.Key == "A"));
            Assert.That(result.Properties.Any(x => x.Key == "B"));
        }