From e3e71a2cbbcb175854398a8bf56a0ece208bde3f Mon Sep 17 00:00:00 2001 From: Christian Kadluba <10721825+ckadluba@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:08:26 +0100 Subject: [PATCH 1/2] Removed obsolete exceptions handlers The sink had exceptions handlers when log events could not be written. Those handlers wrote an error message using Serilog's SelfLog() facility. This is now done by the Serilog Core when a sink's Emit() and EmitBatchAsync() methods throw an exception. This is why can remove our handlers, let exceptions propagate and Serilog Core do the work. --- .../Platform/SqlBulkBatchWriter.cs | 6 ---- .../Platform/SqlInsertStatementWriter.cs | 34 +++++++------------ 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlBulkBatchWriter.cs b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlBulkBatchWriter.cs index 5858b55..7b799dd 100644 --- a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlBulkBatchWriter.cs +++ b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlBulkBatchWriter.cs @@ -60,12 +60,6 @@ public async Task WriteBatch(IEnumerable events) } } } - catch (Exception ex) - { - SelfLog.WriteLine("Unable to write batch of {0} log events to the database due to following error: {1}", - events.Count(), ex); - throw; - } finally { _dataTable.Clear(); diff --git a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlInsertStatementWriter.cs b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlInsertStatementWriter.cs index 30c5caf..3e030ff 100644 --- a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlInsertStatementWriter.cs +++ b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlInsertStatementWriter.cs @@ -43,34 +43,26 @@ public SqlInsertStatementWriter( public async Task WriteEvents(IEnumerable events) { - try + using (var cn = _sqlConnectionFactory.Create()) { - using (var cn = _sqlConnectionFactory.Create()) + await cn.OpenAsync().ConfigureAwait(false); + + foreach (var logEvent in events) { - await cn.OpenAsync().ConfigureAwait(false); + var fields = _logEventDataGenerator.GetColumnsAndValues(logEvent).ToList(); + InitializeSqlCommand(cn, fields); - foreach (var logEvent in events) + var index = 0; + _sqlCommand.ClearParameters(); + foreach (var field in fields) { - var fields = _logEventDataGenerator.GetColumnsAndValues(logEvent).ToList(); - InitializeSqlCommand(cn, fields); - - var index = 0; - _sqlCommand.ClearParameters(); - foreach (var field in fields) - { - _sqlCommand.AddParameter(Invariant($"@P{index}"), field.Value); - index++; - } - - await _sqlCommand.ExecuteNonQueryAsync().ConfigureAwait(false); + _sqlCommand.AddParameter(Invariant($"@P{index}"), field.Value); + index++; } + + await _sqlCommand.ExecuteNonQueryAsync().ConfigureAwait(false); } } - catch (Exception ex) - { - SelfLog.WriteLine("Unable to write log event to the database due to following error: {0}", ex); - throw; - } } /// From 8db290509306d9346ae90fc3e7bb6a13f3648147 Mon Sep 17 00:00:00 2001 From: Christian Kadluba <10721825+ckadluba@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:08:48 +0100 Subject: [PATCH 2/2] Updated CHANGES.md --- CHANGES.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 08154ed..e60df5f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,7 +1,6 @@ # 8.1.0 * Implemented #542: Column option `ResolveHierarchicalPropertyName` to force non-hierarchical handling - -# 8.0.1 +* Removed unnecessary exception handlers and let Serilog Core do the SelfLog() * Refactoring and performance optimizations in batched and audit sink * Create perftest result on release * Updated issue template