|
| 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