Skip to content

Commit 54c09db

Browse files
authored
Merge pull request #56 from colin-young/fb-triggers
Trigger support
2 parents bb2518f + e338ee0 commit 54c09db

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/ColumnOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public ICollection<StandardColumn> Store
6363
}
6464
}
6565

66+
/// <summary>
67+
/// Indicates if triggers should be disabled when inserting log entries.
68+
/// </summary>
69+
public bool DisableTriggers { get; set; }
70+
6671
/// <summary>
6772
/// Additional columns for data storage.
6873
/// </summary>

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSink.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
129129
using (var cn = new SqlConnection(_connectionString))
130130
{
131131
await cn.OpenAsync().ConfigureAwait(false);
132-
using (var copy = new SqlBulkCopy(cn))
132+
using (var copy = _columnOptions.DisableTriggers
133+
? new SqlBulkCopy(cn)
134+
: new SqlBulkCopy(cn, SqlBulkCopyOptions.CheckConstraints | SqlBulkCopyOptions.FireTriggers, null)
135+
)
133136
{
134137
copy.DestinationTableName = _tableName;
135138
foreach (var column in _eventsTable.Columns)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System;
2+
using System.Data.SqlClient;
3+
using System.Diagnostics;
4+
using Dapper;
5+
using FluentAssertions;
6+
using Xunit;
7+
8+
namespace Serilog.Sinks.MSSqlServer.Tests
9+
{
10+
[Collection("LogTest")]
11+
public class TestTriggersOnLogTable
12+
{
13+
[Fact]
14+
public void TestTriggerOnLogTableFire()
15+
{
16+
// arrange
17+
var logTriggerTableName = $"Trigger{DatabaseFixture.LogTableName}Trigger";
18+
var logTableName = $"{DatabaseFixture.LogTableName}WithTrigger";
19+
var loggerConfiguration = new LoggerConfiguration();
20+
Log.Logger = loggerConfiguration.WriteTo.MSSqlServer(
21+
connectionString: DatabaseFixture.LogEventsConnectionString,
22+
tableName: logTableName,
23+
autoCreateSqlTable: true,
24+
batchPostingLimit: 1,
25+
period: TimeSpan.FromSeconds(10),
26+
columnOptions: new ColumnOptions())
27+
.CreateLogger();
28+
29+
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
30+
{
31+
conn.Execute($"CREATE TABLE {logTriggerTableName} ([Id] [UNIQUEIDENTIFIER] NOT NULL, [Data] [NVARCHAR](50) NOT NULL)");
32+
conn.Execute($@"CREATE TRIGGER {logTriggerTableName}NoTrigger ON {logTableName}
33+
AFTER INSERT
34+
AS
35+
BEGIN
36+
INSERT INTO {logTriggerTableName} VALUES (NEWID(), 'Data')
37+
END");
38+
}
39+
40+
// act
41+
const string loggingInformationMessage = "Logging Information message";
42+
Log.Information(loggingInformationMessage);
43+
44+
Log.CloseAndFlush();
45+
46+
// assert
47+
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
48+
{
49+
var logTriggerEvents = conn.Query<TestTriggerEntry>($"SELECT * FROM {logTriggerTableName}");
50+
51+
logTriggerEvents.Should().NotBeNullOrEmpty();
52+
}
53+
}
54+
55+
[Fact]
56+
public void TestOptionsDisableTriggersOnLogTable()
57+
{
58+
// arrange
59+
var options = new ColumnOptions { DisableTriggers = true };
60+
var logTriggerTableName = $"{DatabaseFixture.LogTableName}NoTrigger";
61+
var logTableName = $"{DatabaseFixture.LogTableName}WithTrigger";
62+
var loggerConfiguration = new LoggerConfiguration();
63+
Log.Logger = loggerConfiguration.WriteTo.MSSqlServer(
64+
connectionString: DatabaseFixture.LogEventsConnectionString,
65+
tableName: logTableName,
66+
autoCreateSqlTable: true,
67+
batchPostingLimit: 1,
68+
period: TimeSpan.FromSeconds(10),
69+
columnOptions: options)
70+
.CreateLogger();
71+
72+
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
73+
{
74+
conn.Execute($"CREATE TABLE {logTriggerTableName} ([Id] [UNIQUEIDENTIFIER] NOT NULL, [Data] [NVARCHAR](50) NOT NULL)");
75+
conn.Execute($@"CREATE TRIGGER {logTableName}NoTrigger ON {logTableName}
76+
AFTER INSERT
77+
AS
78+
BEGIN
79+
INSERT INTO {logTriggerTableName} VALUES (NEWID(), 'Data')
80+
END");
81+
}
82+
83+
// act
84+
const string loggingInformationMessage = "Logging Information message";
85+
Log.Information(loggingInformationMessage);
86+
87+
Log.CloseAndFlush();
88+
89+
// assert
90+
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
91+
{
92+
var logTriggerEvents = conn.Query<TestTriggerEntry>($"SELECT * FROM {logTriggerTableName}");
93+
94+
logTriggerEvents.Should().BeEmpty();
95+
}
96+
}
97+
98+
internal class TestTriggerEntry
99+
{
100+
public Guid Id { get; set; }
101+
public string Data { get; set; }
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)