Skip to content

Commit 4db4513

Browse files
committed
Merging SqlLogEventWriter and SqlInsertBatchWriter into SqlInsertStatementWriter
1 parent 00cc7e9 commit 4db4513

File tree

6 files changed

+33
-367
lines changed

6 files changed

+33
-367
lines changed

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Dependencies/SinkDependenciesFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ internal static SinkDependencies Create(
5656
? (ISqlBulkBatchWriter)new SqlBulkBatchWriter(
5757
sinkOptions.TableName, sinkOptions.SchemaName, columnOptions.DisableTriggers,
5858
sqlConnectionFactory, logEventDataGenerator)
59-
: (ISqlBulkBatchWriter)new SqlInsertBatchWriter(
59+
: (ISqlBulkBatchWriter)new SqlInsertStatementWriter(
6060
sinkOptions.TableName, sinkOptions.SchemaName,
6161
sqlConnectionFactory, logEventDataGenerator),
62-
SqlLogEventWriter = new SqlLogEventWriter(
62+
SqlLogEventWriter = new SqlInsertStatementWriter(
6363
sinkOptions.TableName, sinkOptions.SchemaName,
6464
sqlConnectionFactory, logEventDataGenerator)
6565
};

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlInsertBatchWriter.cs renamed to src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlInsertStatementWriter.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data;
4-
using System.Linq;
54
using System.Text;
65
using System.Threading.Tasks;
76
using Serilog.Debugging;
@@ -11,14 +10,14 @@
1110

1211
namespace Serilog.Sinks.MSSqlServer.Platform
1312
{
14-
internal class SqlInsertBatchWriter : ISqlBulkBatchWriter
13+
internal class SqlInsertStatementWriter : ISqlBulkBatchWriter, ISqlLogEventWriter
1514
{
1615
private readonly string _tableName;
1716
private readonly string _schemaName;
1817
private readonly ISqlConnectionFactory _sqlConnectionFactory;
1918
private readonly ILogEventDataGenerator _logEventDataGenerator;
2019

21-
public SqlInsertBatchWriter(
20+
public SqlInsertStatementWriter(
2221
string tableName,
2322
string schemaName,
2423
ISqlConnectionFactory sqlConnectionFactory,
@@ -30,7 +29,11 @@ public SqlInsertBatchWriter(
3029
_logEventDataGenerator = logEventDataGenerator ?? throw new ArgumentNullException(nameof(logEventDataGenerator));
3130
}
3231

33-
public async Task WriteBatch(IEnumerable<LogEvent> events, DataTable dataTable)
32+
public Task WriteBatch(IEnumerable<LogEvent> events, DataTable dataTable) => WriteBatch(events);
33+
34+
public void WriteEvent(LogEvent logEvent) => WriteBatch(new[] { logEvent }).GetAwaiter().GetResult();
35+
36+
public async Task WriteBatch(IEnumerable<LogEvent> events)
3437
{
3538
try
3639
{
@@ -77,8 +80,7 @@ public async Task WriteBatch(IEnumerable<LogEvent> events, DataTable dataTable)
7780
}
7881
catch (Exception ex)
7982
{
80-
SelfLog.WriteLine("Unable to write batch of {0} log events to the database due to following error: {1}",
81-
events.Count(), ex);
83+
SelfLog.WriteLine("Unable to write log event to the database due to following error: {0}", ex);
8284
throw;
8385
}
8486
}

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

Lines changed: 0 additions & 78 deletions
This file was deleted.

test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Dependencies/SinkDependenciesFactoryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void CreatesSinkDependenciesWithSqlLogEventWriter()
7070

7171
// Assert
7272
Assert.NotNull(result.SqlLogEventWriter);
73-
Assert.IsType<SqlLogEventWriter>(result.SqlLogEventWriter);
73+
Assert.IsType<SqlInsertStatementWriter>(result.SqlLogEventWriter);
7474
}
7575

7676
[Fact]

test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Platform/SqlInsertBatchWriterTests.cs renamed to test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Platform/SqlInsertStatementWriterTests.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
namespace Serilog.Sinks.MSSqlServer.Tests.Platform
1313
{
1414
[Trait(TestCategory.TraitName, TestCategory.Unit)]
15-
public class SqlInsertBatchWriterTests
15+
public class SqlInsertStatementWriterTests
1616
{
1717
private const string _tableName = "TestTableName";
1818
private const string _schemaName = "TestSchemaName";
1919
private readonly Mock<ISqlConnectionFactory> _sqlConnectionFactoryMock;
2020
private readonly Mock<ILogEventDataGenerator> _logEventDataGeneratorMock;
2121
private readonly Mock<ISqlConnectionWrapper> _sqlConnectionWrapperMock;
2222
private readonly Mock<ISqlCommandWrapper> _sqlCommandWrapperMock;
23-
private readonly SqlInsertBatchWriter _sut;
23+
private readonly SqlInsertStatementWriter _sut;
2424

25-
public SqlInsertBatchWriterTests()
25+
public SqlInsertStatementWriterTests()
2626
{
2727
_sqlConnectionFactoryMock = new Mock<ISqlConnectionFactory>();
2828
_logEventDataGeneratorMock = new Mock<ILogEventDataGenerator>();
@@ -32,31 +32,31 @@ public SqlInsertBatchWriterTests()
3232
_sqlConnectionFactoryMock.Setup(f => f.Create()).Returns(_sqlConnectionWrapperMock.Object);
3333
_sqlConnectionWrapperMock.Setup(c => c.CreateCommand()).Returns(_sqlCommandWrapperMock.Object);
3434

35-
_sut = new SqlInsertBatchWriter(_tableName, _schemaName, _sqlConnectionFactoryMock.Object, _logEventDataGeneratorMock.Object);
35+
_sut = new SqlInsertStatementWriter(_tableName, _schemaName, _sqlConnectionFactoryMock.Object, _logEventDataGeneratorMock.Object);
3636
}
3737

3838
[Fact]
3939
public void InitializeWithoutTableNameThrows()
4040
{
41-
Assert.Throws<ArgumentNullException>(() => new SqlInsertBatchWriter(null, _schemaName, _sqlConnectionFactoryMock.Object, _logEventDataGeneratorMock.Object));
41+
Assert.Throws<ArgumentNullException>(() => new SqlInsertStatementWriter(null, _schemaName, _sqlConnectionFactoryMock.Object, _logEventDataGeneratorMock.Object));
4242
}
4343

4444
[Fact]
4545
public void InitializeWithoutSchemaNameThrows()
4646
{
47-
Assert.Throws<ArgumentNullException>(() => new SqlInsertBatchWriter(_tableName, null, _sqlConnectionFactoryMock.Object, _logEventDataGeneratorMock.Object));
47+
Assert.Throws<ArgumentNullException>(() => new SqlInsertStatementWriter(_tableName, null, _sqlConnectionFactoryMock.Object, _logEventDataGeneratorMock.Object));
4848
}
4949

5050
[Fact]
5151
public void InitializeWithoutSqlConnectionFactoryThrows()
5252
{
53-
Assert.Throws<ArgumentNullException>(() => new SqlInsertBatchWriter(_tableName, _schemaName, null, _logEventDataGeneratorMock.Object));
53+
Assert.Throws<ArgumentNullException>(() => new SqlInsertStatementWriter(_tableName, _schemaName, null, _logEventDataGeneratorMock.Object));
5454
}
5555

5656
[Fact]
5757
public void InitializeWithoutLogEventDataGeneratorThrows()
5858
{
59-
Assert.Throws<ArgumentNullException>(() => new SqlInsertBatchWriter(_tableName, _schemaName, _sqlConnectionFactoryMock.Object, null));
59+
Assert.Throws<ArgumentNullException>(() => new SqlInsertStatementWriter(_tableName, _schemaName, _sqlConnectionFactoryMock.Object, null));
6060
}
6161

6262
[Fact]
@@ -66,7 +66,7 @@ public async Task WriteBatchCallsSqlConnectionFactoryCreate()
6666
var logEvents = CreateLogEvents();
6767

6868
// Act
69-
await _sut.WriteBatch(logEvents, null);
69+
await _sut.WriteBatch(logEvents);
7070

7171
// Assert
7272
_sqlConnectionFactoryMock.Verify(f => f.Create(), Times.Once);
@@ -79,7 +79,7 @@ public async Task WriteBatchCallsSqlConnectionWrapperOpenAsync()
7979
var logEvents = CreateLogEvents();
8080

8181
// Act
82-
await _sut.WriteBatch(logEvents, null);
82+
await _sut.WriteBatch(logEvents);
8383

8484
// Assert
8585
_sqlConnectionWrapperMock.Verify(c => c.OpenAsync(), Times.Once);
@@ -92,7 +92,7 @@ public async Task WriteBatchCallsSqlConnectionWrappeCreateCommand()
9292
var logEvents = CreateLogEvents();
9393

9494
// Act
95-
await _sut.WriteBatch(logEvents, null);
95+
await _sut.WriteBatch(logEvents);
9696

9797
// Assert
9898
_sqlConnectionWrapperMock.Verify(c => c.CreateCommand(), Times.Exactly(logEvents.Count));
@@ -105,7 +105,7 @@ public async Task WriteBatchSetsSqlCommandWrapperCommandTypeText()
105105
var logEvents = CreateLogEvents();
106106

107107
// Act
108-
await _sut.WriteBatch(logEvents, null);
108+
await _sut.WriteBatch(logEvents);
109109

110110
// Assert
111111
_sqlCommandWrapperMock.VerifySet(c => c.CommandType = System.Data.CommandType.Text);
@@ -129,7 +129,7 @@ public async Task WriteBatchCallsSqlCommandWrapperAddParameterForEachField()
129129
.Returns(fieldsAndValues);
130130

131131
// Act
132-
await _sut.WriteBatch(new[] { logEvent }, null);
132+
await _sut.WriteBatch(new[] { logEvent });
133133

134134
// Assert
135135
_sqlCommandWrapperMock.Verify(c => c.AddParameter("@P0", field1Value), Times.Once);
@@ -153,7 +153,7 @@ public async Task WriteBatchSetsSqlCommandWrapperCommandTextToSqlInsertWithCorre
153153
.Returns(fieldsAndValues);
154154

155155
// Act
156-
await _sut.WriteBatch(new[] { logEvent }, null);
156+
await _sut.WriteBatch(new[] { logEvent });
157157

158158
// Assert
159159
_sqlCommandWrapperMock.VerifySet(c => c.CommandText = expectedSqlCommandText);
@@ -166,7 +166,7 @@ public async Task WriteBatchCallsSqlCommandWrapperExecuteNonQueryAsync()
166166
var logEvents = CreateLogEvents();
167167

168168
// Act
169-
await _sut.WriteBatch(logEvents, null);
169+
await _sut.WriteBatch(logEvents);
170170

171171
// Assert
172172
_sqlCommandWrapperMock.Verify(c => c.ExecuteNonQueryAsync(), Times.Exactly(logEvents.Count));
@@ -179,7 +179,7 @@ public async Task WriteBatchCallsSqlCommandWrapperDispose()
179179
var logEvents = CreateLogEvents();
180180

181181
// Act
182-
await _sut.WriteBatch(logEvents, null);
182+
await _sut.WriteBatch(logEvents);
183183

184184
// Assert
185185
_sqlCommandWrapperMock.Verify(c => c.Dispose(), Times.Exactly(logEvents.Count));
@@ -192,7 +192,7 @@ public async Task WriteBatchCallsLogEventDataGeneratorGetColumnsAndValuesForEach
192192
var logEvents = CreateLogEvents();
193193

194194
// Act
195-
await _sut.WriteBatch(logEvents, null).ConfigureAwait(false);
195+
await _sut.WriteBatch(logEvents).ConfigureAwait(false);
196196

197197
// Assert
198198
_logEventDataGeneratorMock.Verify(c => c.GetColumnsAndValues(logEvents[0]), Times.Once);
@@ -207,7 +207,7 @@ public async Task WriteBatchRethrowsIfSqlConnectionFactoryCreateThrows()
207207
var logEvents = CreateLogEvents();
208208

209209
// Act + assert
210-
await Assert.ThrowsAsync<InvalidOperationException>(() => _sut.WriteBatch(logEvents, null));
210+
await Assert.ThrowsAsync<InvalidOperationException>(() => _sut.WriteBatch(logEvents));
211211
}
212212

213213
[Fact]
@@ -218,7 +218,7 @@ public async Task WriteBatchRethrowsIfSqlConnectionCreateCommandThrows()
218218
var logEvents = CreateLogEvents();
219219

220220
// Act + assert
221-
await Assert.ThrowsAsync<InvalidOperationException>(() => _sut.WriteBatch(logEvents, null));
221+
await Assert.ThrowsAsync<InvalidOperationException>(() => _sut.WriteBatch(logEvents));
222222
}
223223

224224
[Fact]
@@ -229,7 +229,7 @@ public async Task WriteBatchRethrowsIfLogEventDataGeneratorGetColumnsAndValuesTh
229229
var logEvents = CreateLogEvents();
230230

231231
// Act + assert
232-
await Assert.ThrowsAsync<InvalidOperationException>(() => _sut.WriteBatch(logEvents, null));
232+
await Assert.ThrowsAsync<InvalidOperationException>(() => _sut.WriteBatch(logEvents));
233233
}
234234

235235
[Fact]
@@ -243,7 +243,7 @@ public async Task WriteBatchRethrowsIfSqlCommandAddParameterThrows()
243243
var logEvents = CreateLogEvents();
244244

245245
// Act + assert
246-
await Assert.ThrowsAsync<InvalidOperationException>(() => _sut.WriteBatch(logEvents, null));
246+
await Assert.ThrowsAsync<InvalidOperationException>(() => _sut.WriteBatch(logEvents));
247247
}
248248

249249
[Fact]
@@ -254,7 +254,7 @@ public async Task WriteBatchRethrowsIfSqlCommandExecuteNonQueryAsyncThrows()
254254
var logEvents = CreateLogEvents();
255255

256256
// Act + assert
257-
await Assert.ThrowsAsync<InvalidOperationException>(() => _sut.WriteBatch(logEvents, null));
257+
await Assert.ThrowsAsync<InvalidOperationException>(() => _sut.WriteBatch(logEvents));
258258
}
259259

260260
private static List<LogEvent> CreateLogEvents()

0 commit comments

Comments
 (0)