From 48d56da92accdc5f28f2a94ddac338c4f65a698a Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Tue, 12 May 2015 19:54:34 +1000 Subject: [PATCH 1/5] Update Serilog to 1.5 This bumps the assembly version from 1.4.0.0 to 1.5.0.0 --- src/Microsoft.Framework.Logging.Serilog/project.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Framework.Logging.Serilog/project.json b/src/Microsoft.Framework.Logging.Serilog/project.json index 44eac6b8..82030d6d 100644 --- a/src/Microsoft.Framework.Logging.Serilog/project.json +++ b/src/Microsoft.Framework.Logging.Serilog/project.json @@ -3,7 +3,7 @@ "dependencies": { "Microsoft.Framework.Logging.Abstractions": "1.0.0-*", "Microsoft.Framework.NotNullAttribute.Sources": { "type": "build", "version": "1.0.0-*" }, - "Serilog": "1.4.14" + "Serilog": "1.5.5" }, "frameworks": { "dnx451": { From 81896c7e6f3a3b7421ee772b1fda915ab291b2ec Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Tue, 12 May 2015 19:56:04 +1000 Subject: [PATCH 2/5] Handle the Debug level in the Serilog logger Debug in DNX maps to Serilog's Verbose, and vice-versa. The change includes a fall-through to make sure any unrecognised levels map also to the lowest (most harmless) level instead of throwing a potentially CPU-draining exception. --- src/Microsoft.Framework.Logging.Serilog/SerilogLogger.cs | 5 +++-- test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.Framework.Logging.Serilog/SerilogLogger.cs b/src/Microsoft.Framework.Logging.Serilog/SerilogLogger.cs index dc47173b..09396946 100644 --- a/src/Microsoft.Framework.Logging.Serilog/SerilogLogger.cs +++ b/src/Microsoft.Framework.Logging.Serilog/SerilogLogger.cs @@ -88,9 +88,10 @@ private LogEventLevel ConvertLevel(LogLevel logLevel) case LogLevel.Information: return LogEventLevel.Information; case LogLevel.Verbose: - return LogEventLevel.Verbose; + return LogEventLevel.Debug; + case LogLevel.Debug: default: - throw new NotSupportedException(); + return LogEventLevel.Verbose; } } diff --git a/test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs b/test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs index 806dfe37..d54a2f21 100644 --- a/test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs +++ b/test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs @@ -37,8 +37,10 @@ private LoggerConfiguration SetMinLevel(LoggerConfiguration serilog, LogLevel lo { switch (logLevel) { - case LogLevel.Verbose: + case LogLevel.Debug: return serilog.MinimumLevel.Verbose(); + case LogLevel.Verbose: + return serilog.MinimumLevel.Debug(); case LogLevel.Information: return serilog.MinimumLevel.Information(); case LogLevel.Warning: From f0dc1be96ff586ae130a1d019c0b4c853043b105 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Tue, 12 May 2015 21:01:45 +1000 Subject: [PATCH 3/5] Use the Serilog infrastructure for Exception and Message - Converts "{OriginalFormat}" back to the message template - Passes the Exception through to the Serilog logger rather than pre-formatting - Defers to user-supplied enrichers rather than enriching every event with exception properties --- .../SerilogLogger.cs | 80 +++++-------------- 1 file changed, 19 insertions(+), 61 deletions(-) diff --git a/src/Microsoft.Framework.Logging.Serilog/SerilogLogger.cs b/src/Microsoft.Framework.Logging.Serilog/SerilogLogger.cs index 09396946..55ce6142 100644 --- a/src/Microsoft.Framework.Logging.Serilog/SerilogLogger.cs +++ b/src/Microsoft.Framework.Logging.Serilog/SerilogLogger.cs @@ -44,35 +44,38 @@ public void Log(LogLevel logLevel, int eventId, object state, Exception exceptio } var logger = _logger; + string messageTemplate = null; - var message = string.Empty; - if (formatter != null) + var structure = state as ILogValues; + if (structure != null) { - message = formatter(state, exception); + foreach (var property in structure.GetValues()) + { + if (property.Key == "{OriginalFormat}" && property.Value is string) + { + messageTemplate = (string)property.Value; + } + + logger = logger.ForContext(property.Key, property.Value); + } } - else + + if (messageTemplate == null && state != null) { - message = LogFormatter.Formatter(state, exception); + messageTemplate = LogFormatter.Formatter(state, null); } - if (string.IsNullOrEmpty(message)) + + if (string.IsNullOrEmpty(messageTemplate)) { return; } + if (eventId != 0) { logger = logger.ForContext("EventId", eventId, false); } - var structure = state as ILogValues; - if (structure != null) - { - logger = logger.ForContext(new[] { new StructureEnricher(structure) }); - } - if (exception != null) - { - logger = logger.ForContext(new[] { new ExceptionEnricher(exception) }); - } - logger.Write(level, "{Message:l}", message); + logger.Write(level, exception, messageTemplate); } private LogEventLevel ConvertLevel(LogLevel logLevel) @@ -94,50 +97,5 @@ private LogEventLevel ConvertLevel(LogLevel logLevel) return LogEventLevel.Verbose; } } - - private class StructureEnricher : ILogEventEnricher - { - private readonly ILogValues _structure; - - public StructureEnricher(ILogValues structure) - { - _structure = structure; - } - - public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) - { - foreach (var value in _structure.GetValues()) - { - logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty( - value.Key, - value.Value)); - } - } - } - - private sealed class ExceptionEnricher : ILogEventEnricher - { - private readonly Exception _exception; - - public ExceptionEnricher(Exception exception) - { - _exception = exception; - } - - public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) - { - logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty( - "Exception", - _exception.ToString())); - - logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty( - "ExceptionType", - _exception.GetType().FullName)); - - logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty( - "ExceptionMessage", - _exception.Message)); - } - } } } \ No newline at end of file From c65a518880790cc0d51c2019a0f62065511ddf7a Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Tue, 12 May 2015 21:53:01 +1000 Subject: [PATCH 4/5] Bring tests into line with changes --- .../SerilogLoggerTest.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs b/test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs index d54a2f21..87ea6107 100644 --- a/test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs +++ b/test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs @@ -78,6 +78,7 @@ public void LogsCorrectLevel() var sink = t.Item2; // Act + logger.Log(LogLevel.Debug, 0, _state, null, null); logger.Log(LogLevel.Verbose, 0, _state, null, null); logger.Log(LogLevel.Information, 0, _state, null, null); logger.Log(LogLevel.Warning, 0, _state, null, null); @@ -85,12 +86,13 @@ public void LogsCorrectLevel() logger.Log(LogLevel.Critical, 0, _state, null, null); // Assert - Assert.Equal(5, sink.Writes.Count); + Assert.Equal(6, sink.Writes.Count); Assert.Equal(LogEventLevel.Verbose, sink.Writes[0].Level); - Assert.Equal(LogEventLevel.Information, sink.Writes[1].Level); - Assert.Equal(LogEventLevel.Warning, sink.Writes[2].Level); - Assert.Equal(LogEventLevel.Error, sink.Writes[3].Level); - Assert.Equal(LogEventLevel.Fatal, sink.Writes[4].Level); + Assert.Equal(LogEventLevel.Debug, sink.Writes[1].Level); + Assert.Equal(LogEventLevel.Information, sink.Writes[2].Level); + Assert.Equal(LogEventLevel.Warning, sink.Writes[3].Level); + Assert.Equal(LogEventLevel.Error, sink.Writes[4].Level); + Assert.Equal(LogEventLevel.Fatal, sink.Writes[5].Level); } [Theory] @@ -147,14 +149,10 @@ public void LogsCorrectMessage() // Act logger.Log(LogLevel.Information, 0, null, null, null); logger.Log(LogLevel.Information, 0, _state, null, null); - logger.Log(LogLevel.Information, 0, _state, exception, null); - logger.Log(LogLevel.Information, 0, _state, exception, TheMessageAndError); // Assert - Assert.Equal(3, sink.Writes.Count); + Assert.Equal(1, sink.Writes.Count); Assert.Equal(_state, sink.Writes[0].RenderMessage()); - Assert.Equal(_state + Environment.NewLine + exception, sink.Writes[1].RenderMessage()); - Assert.Equal(TheMessageAndError(_state, exception), sink.Writes[2].RenderMessage()); } [Fact] From edcc7229cff76c18dbfa1f5b1f087bdfcce517a8 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Tue, 12 May 2015 22:24:45 +1000 Subject: [PATCH 5/5] Fix the level mapping test to cover Debug messages --- test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs b/test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs index 87ea6107..0b777344 100644 --- a/test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs +++ b/test/Microsoft.Framework.Logging.Test/SerilogLoggerTest.cs @@ -73,7 +73,7 @@ public void LogsWhenNullFilterGiven() public void LogsCorrectLevel() { // Arrange - var t = SetUp(LogLevel.Verbose); + var t = SetUp(LogLevel.Debug); var logger = t.Item1; var sink = t.Item2;