I'm using Serilog in my .NET application along with Serilog.Sinks.GoogleCloudLogging
to send logs to Datadog. I would like to automatically elevate all SQL exceptions (SqlException
) to the Fatal
(or Critical
) log level, regardless of how they are initially logged (e.g., Error
or Warning
).
I've tried using enrichers and filters, but it seems the log level isn't effectively modified before it reaches the sink, and logs still appear with their original log level in Datadog (e.g., Error instead of Fatal).
What I've tried:
SqlException
log events. However, the log level remains unchanged in Google Cloud Logging. public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (logEvent.Exception is SqlException)
{
logEvent = new LogEvent(logEvent.Timestamp, LogEventLevel.Fatal, logEvent.Exception, logEvent.MessageTemplate,
logEvent.Properties.Select(p => new LogEventProperty(p.Key, p.Value))
);
}
}
Filters: I applied filters to elevate SqlException
logs to Fatal level, but this didn't work as expected with the Google Cloud Logging sink.
ExceptionHandlingMiddleware
: I also tried a middleware to catch exceptions globally and modify the log level before logging, but the level still shows as Error instead of Fatal in Google Cloud.
Is there a way to modify the log level for all SqlException
log entries to be logged as Fatal when using Serilog.Sinks.GoogleCloudLogging
?
I was able to accomplish that goal by setting a custom LogEventSink
:
public class CustomEventSink(ILogEventSink innerSink) : ILogEventSink
{
public void Emit(LogEvent logEvent)
{
logEvent = AdjustLogLevelForSqlException(logEvent);
innerSink.Emit(logEvent);
}
private LogEvent AdjustLogLevelForSqlException(LogEvent logEvent)
{
if (logEvent.Exception is SqlException)
{
return new LogEvent(logEvent.Timestamp, LogEventLevel.Fatal, logEvent.Exception, logEvent.MessageTemplate,
logEvent.Properties.Select(p => new LogEventProperty(p.Key, p.Value))
);
}
return logEvent;
}
}
Then in the Program.cs
:
var loggerConfiguration = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.Enrich.FromLogContext();
var customEventSink = new CustomEventSink(loggerConfiguration.CreateLogger());
var appLogger = new LoggerConfiguration()
.WriteTo.Sink(customEventSink)
.CreateLogger();
Now, on Datadog the severity of my SQL exceptions are marked as CRITICAL.