Skip to content

Commit 4949611

Browse files
committed
Bugfix: also write SelfLog and rethrow in SqlBulkBatchWriter if and exception is thrown during preparing of the log events DataTable.
1 parent a139051 commit 4949611

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlBulkBatchWriter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ public SqlBulkBatchWriter(
3434

3535
public async Task WriteBatch(IEnumerable<LogEvent> events, DataTable dataTable)
3636
{
37-
// Copy the events to the data table
38-
FillDataTable(events, dataTable);
39-
4037
try
4138
{
39+
FillDataTable(events, dataTable);
40+
4241
using (var cn = _sqlConnectionFactory.Create())
4342
{
4443
await cn.OpenAsync().ConfigureAwait(false);
@@ -61,7 +60,6 @@ public async Task WriteBatch(IEnumerable<LogEvent> events, DataTable dataTable)
6160
}
6261
finally
6362
{
64-
// Processed the items, clear for the next run
6563
dataTable.Clear();
6664
}
6765
}

test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Platform/SqlBulkBatchWriterTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,18 @@ public async Task WriteBatchClearsDataTable()
177177
Assert.Empty(_dataTable.Rows);
178178
}
179179

180+
[Fact]
181+
public void WriteBatchRethrowsIfLogEventDataGeneratorMockGetColumnsAndValuesThrows()
182+
{
183+
// Arrange
184+
_logEventDataGeneratorMock.Setup(d => d.GetColumnsAndValues(It.IsAny<LogEvent>()))
185+
.Callback(() => throw new Exception());
186+
var logEvents = CreateLogEvents();
187+
188+
// Act + assert
189+
Assert.ThrowsAsync<Exception>(() => _sut.WriteBatch(logEvents, _dataTable));
190+
}
191+
180192
[Fact]
181193
public void WriteBatchRethrowsIfSqlConnectionFactoryCreateThrows()
182194
{

0 commit comments

Comments
 (0)