Skip to content
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

Do not swallow exception from Audit #218

Merged
merged 2 commits into from
Mar 12, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
rewrite
  • Loading branch information
sungam3r committed Mar 11, 2023
commit 6da75a2b8608d965d130fee72deccd8f2d78a862
28 changes: 14 additions & 14 deletions src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,23 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
return;
}

bool parsed = false;
LogEvent? evt = null;
try
{
Write(level, eventId, state, exception, formatter, out parsed);
evt = PrepareWrite(level, eventId, state, exception, formatter);
}
catch (Exception ex) when (!parsed)
catch (Exception ex)
{
SelfLog.WriteLine($"Failed to write event through {typeof(SerilogLogger).Name}: {ex}");
}

// Do not swallow exceptions from here because Serilog takes care of them in case of WriteTo and throws them back to the caller in case of AuditTo.
if (evt != null)
_logger.Write(evt);
}

void Write<TState>(LogEventLevel level, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter, out bool parsed)
LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
parsed = false;
var logger = _logger;
string? messageTemplate = null;

var properties = new List<LogEventProperty>();
Expand All @@ -103,17 +105,17 @@ void Write<TState>(LogEventLevel level, EventId eventId, TState state, Exception
}
else if (property.Key.StartsWith("@"))
{
if (logger.BindProperty(GetKeyWithoutFirstSymbol(DestructureDictionary, property.Key), property.Value, true, out var destructured))
if (_logger.BindProperty(GetKeyWithoutFirstSymbol(DestructureDictionary, property.Key), property.Value, true, out var destructured))
properties.Add(destructured);
}
else if (property.Key.StartsWith("$"))
{
if (logger.BindProperty(GetKeyWithoutFirstSymbol(StringifyDictionary, property.Key), property.Value?.ToString(), true, out var stringified))
if (_logger.BindProperty(GetKeyWithoutFirstSymbol(StringifyDictionary, property.Key), property.Value?.ToString(), true, out var stringified))
properties.Add(stringified);
}
else
{
if (logger.BindProperty(property.Key, property.Value, false, out var bound))
if (_logger.BindProperty(property.Key, property.Value, false, out var bound))
properties.Add(bound);
}
}
Expand All @@ -124,7 +126,7 @@ void Write<TState>(LogEventLevel level, EventId eventId, TState state, Exception
if (messageTemplate == null && !stateTypeInfo.IsGenericType)
{
messageTemplate = "{" + stateType.Name + ":l}";
if (logger.BindProperty(stateType.Name, AsLoggableValue(state, formatter), false, out var stateTypeProperty))
if (_logger.BindProperty(stateType.Name, AsLoggableValue(state, formatter), false, out var stateTypeProperty))
properties.Add(stateTypeProperty);
}
}
Expand All @@ -145,7 +147,7 @@ void Write<TState>(LogEventLevel level, EventId eventId, TState state, Exception

if (propertyName != null)
{
if (logger.BindProperty(propertyName, AsLoggableValue(state, formatter!), false, out var property))
if (_logger.BindProperty(propertyName, AsLoggableValue(state, formatter!), false, out var property))
properties.Add(property);
}
}
Expand All @@ -154,9 +156,7 @@ void Write<TState>(LogEventLevel level, EventId eventId, TState state, Exception
properties.Add(CreateEventIdProperty(eventId));

var parsedTemplate = MessageTemplateParser.Parse(messageTemplate ?? "");
var evt = new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, properties);
parsed = true;
logger.Write(evt);
return new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, properties);
}

static object? AsLoggableValue<TState>(TState state, Func<TState, Exception, string> formatter)
Expand Down