Skip to content

Commit cf6caad

Browse files
committed
Resolves #164 - suppress exceptions from FormattedLogValues
1 parent 56418c3 commit cf6caad

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

serilog-extensions-logging.sln.DotSettings

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
<s:Boolean x:Key="/Default/UserDictionary/Words/=Loggable/@EntryIndexedValue">True</s:Boolean>
99
<s:Boolean x:Key="/Default/UserDictionary/Words/=Nonscalar/@EntryIndexedValue">True</s:Boolean>
1010
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean>
11-
<s:Boolean x:Key="/Default/UserDictionary/Words/=sobj/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
11+
<s:Boolean x:Key="/Default/UserDictionary/Words/=sobj/@EntryIndexedValue">True</s:Boolean>
12+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Stringification/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Serilog.Events;
1010
using FrameworkLogger = Microsoft.Extensions.Logging.ILogger;
1111
using System.Reflection;
12+
using Serilog.Debugging;
1213
using Serilog.Parsing;
1314

1415
namespace Serilog.Extensions.Logging
@@ -60,6 +61,18 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
6061
return;
6162
}
6263

64+
try
65+
{
66+
Write(level, eventId, state, exception, formatter);
67+
}
68+
catch (Exception ex)
69+
{
70+
SelfLog.WriteLine($"Failed to write event through {typeof(SerilogLogger).Name}: {ex}");
71+
}
72+
}
73+
74+
void Write<TState>(LogEventLevel level, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
75+
{
6376
var logger = _logger;
6477
string messageTemplate = null;
6578

test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void StringifyScopeProperty()
167167
{
168168
var (logger, sink) = SetUp(LogLevel.Trace);
169169

170-
using (logger.BeginScope("{$values}", new int[] { 1, 2, 3, 4 }))
170+
using (logger.BeginScope("{$values}", new [] { 1, 2, 3, 4 }))
171171
{
172172
logger.Log(LogLevel.Information, 0, TestMessage, null, null);
173173
}
@@ -272,6 +272,7 @@ public void CarriesEventIdIfNonzero()
272272
public void WhenDisposeIsFalseProvidedLoggerIsNotDisposed()
273273
{
274274
var logger = new DisposeTrackingLogger();
275+
// ReSharper disable once RedundantArgumentDefaultValue
275276
var provider = new SerilogLoggerProvider(logger, false);
276277
provider.Dispose();
277278
Assert.False(logger.IsDisposed);
@@ -439,5 +440,15 @@ public void LowAndHighNumberedEventIdsAreMapped(int id)
439440
var scalar = Assert.IsType<ScalarValue>(idValue);
440441
Assert.Equal(id, scalar.Value);
441442
}
443+
444+
[Fact]
445+
public void MismatchedMessageTemplateParameterCountIsHandled()
446+
{
447+
var (logger, sink) = SetUp(LogLevel.Trace);
448+
449+
logger.LogInformation("Some test message with {Two} {Properties}", "OneProperty");
450+
451+
Assert.Equal(0, sink.Writes.Count);
452+
}
442453
}
443454
}

0 commit comments

Comments
 (0)