Skip to content

Resolves #164 - suppress exceptions from FormattedLogValues #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion serilog-extensions-logging.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Loggable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Nonscalar/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=sobj/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/UserDictionary/Words/=sobj/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Stringification/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
13 changes: 13 additions & 0 deletions src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Serilog.Events;
using FrameworkLogger = Microsoft.Extensions.Logging.ILogger;
using System.Reflection;
using Serilog.Debugging;
using Serilog.Parsing;

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

try
{
Write(level, eventId, state, exception, formatter);
}
catch (Exception ex)
{
SelfLog.WriteLine($"Failed to write event through {typeof(SerilogLogger).Name}: {ex}");
}
}

void Write<TState>(LogEventLevel level, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var logger = _logger;
string messageTemplate = null;

Expand Down
13 changes: 12 additions & 1 deletion test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void StringifyScopeProperty()
{
var (logger, sink) = SetUp(LogLevel.Trace);

using (logger.BeginScope("{$values}", new int[] { 1, 2, 3, 4 }))
using (logger.BeginScope("{$values}", new [] { 1, 2, 3, 4 }))
{
logger.Log(LogLevel.Information, 0, TestMessage, null, null);
}
Expand Down Expand Up @@ -272,6 +272,7 @@ public void CarriesEventIdIfNonzero()
public void WhenDisposeIsFalseProvidedLoggerIsNotDisposed()
{
var logger = new DisposeTrackingLogger();
// ReSharper disable once RedundantArgumentDefaultValue
var provider = new SerilogLoggerProvider(logger, false);
provider.Dispose();
Assert.False(logger.IsDisposed);
Expand Down Expand Up @@ -439,5 +440,15 @@ public void LowAndHighNumberedEventIdsAreMapped(int id)
var scalar = Assert.IsType<ScalarValue>(idValue);
Assert.Equal(id, scalar.Value);
}

[Fact]
public void MismatchedMessageTemplateParameterCountIsHandled()
{
var (logger, sink) = SetUp(LogLevel.Trace);

logger.LogInformation("Some test message with {Two} {Properties}", "OneProperty");

Assert.Equal(0, sink.Writes.Count);
}
}
}