|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data.SqlClient; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Dapper; |
| 9 | +using Xunit; |
| 10 | +using FluentAssertions; |
| 11 | +using Serilog.Events; |
| 12 | + |
| 13 | +namespace Serilog.Sinks.MSSqlServer.Tests |
| 14 | +{ |
| 15 | + public class CustomStandardColumnNames : IClassFixture<DatabaseFixture> |
| 16 | + { |
| 17 | + [Fact] |
| 18 | + public void CustomIdColumn() |
| 19 | + { |
| 20 | + // arrange |
| 21 | + var options = new ColumnOptions(); |
| 22 | + var customIdName = "CustomIdName"; |
| 23 | + options.Id.ColumnName = customIdName; |
| 24 | + |
| 25 | + // act |
| 26 | + var logTableName = $"{DatabaseFixture.LogTableName}CustomId"; |
| 27 | + var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, logTableName, 1, TimeSpan.FromSeconds(1), null, true, options); |
| 28 | + |
| 29 | + // assert |
| 30 | + using (var conn = new SqlConnection(DatabaseFixture.MasterConnectionString)) |
| 31 | + { |
| 32 | + conn.Execute($"use {DatabaseFixture.Database}"); |
| 33 | + var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{logTableName}'"); |
| 34 | + var infoSchema = logEvents as InfoSchema[] ?? logEvents.ToArray(); |
| 35 | + |
| 36 | + infoSchema.Should().Contain(columns => columns.ColumnName == customIdName); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void DefaultIdColumn() |
| 42 | + { |
| 43 | + // arrange |
| 44 | + var options = new ColumnOptions(); |
| 45 | + |
| 46 | + // act |
| 47 | + var logTableName = $"{DatabaseFixture.LogTableName}DefaultId"; |
| 48 | + var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, logTableName, 1, TimeSpan.FromSeconds(1), null, true, options); |
| 49 | + |
| 50 | + // assert |
| 51 | + using (var conn = new SqlConnection(DatabaseFixture.MasterConnectionString)) |
| 52 | + { |
| 53 | + conn.Execute($"use {DatabaseFixture.Database}"); |
| 54 | + var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{logTableName}'"); |
| 55 | + var infoSchema = logEvents as InfoSchema[] ?? logEvents.ToArray(); |
| 56 | + |
| 57 | + infoSchema.Should().Contain(columns => columns.ColumnName == "Id"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void TableCreatedWithCustomNames() |
| 63 | + { |
| 64 | + // arrange |
| 65 | + var options = new ColumnOptions(); |
| 66 | + var standardNames = new List<string> { "CustomMessage", "CustomMessageTemplate", "CustomLevel", "CustomTimeStamp", "CustomException", "CustomProperties" }; |
| 67 | + |
| 68 | + options.Message.ColumnName = "CustomMessage"; |
| 69 | + options.MessageTemplate.ColumnName = "CustomMessageTemplate"; |
| 70 | + options.Level.ColumnName = "CustomLevel"; |
| 71 | + options.TimeStamp.ColumnName = "CustomTimeStamp"; |
| 72 | + options.Exception.ColumnName = "CustomException"; |
| 73 | + options.Properties.ColumnName = "CustomProperties"; |
| 74 | + |
| 75 | + // act |
| 76 | + var logTableName = $"{DatabaseFixture.LogTableName}Custom"; |
| 77 | + var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, logTableName, 1, TimeSpan.FromSeconds(1), null, true, options); |
| 78 | + |
| 79 | + // assert |
| 80 | + using (var conn = new SqlConnection(DatabaseFixture.MasterConnectionString)) |
| 81 | + { |
| 82 | + conn.Execute($"use {DatabaseFixture.Database}"); |
| 83 | + var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{logTableName}'"); |
| 84 | + var infoSchema = logEvents as InfoSchema[] ?? logEvents.ToArray(); |
| 85 | + |
| 86 | + foreach (var column in standardNames) |
| 87 | + { |
| 88 | + infoSchema.Should().Contain(columns => columns.ColumnName == column); |
| 89 | + } |
| 90 | + |
| 91 | + infoSchema.Should().Contain(columns => columns.ColumnName == "Id"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void TableCreatedWithDefaultNames() |
| 97 | + { |
| 98 | + // arrange |
| 99 | + var options = new ColumnOptions(); |
| 100 | + var standardNames = new List<string> { "Message", "MessageTemplate", "Level", "TimeStamp", "Exception", "Properties" }; |
| 101 | + |
| 102 | + // act |
| 103 | + var logTableName = $"{DatabaseFixture.LogTableName}DefaultStandard"; |
| 104 | + var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, logTableName, 1, TimeSpan.FromSeconds(1), null, true, options); |
| 105 | + |
| 106 | + // assert |
| 107 | + using (var conn = new SqlConnection(DatabaseFixture.MasterConnectionString)) |
| 108 | + { |
| 109 | + conn.Execute($"use {DatabaseFixture.Database}"); |
| 110 | + var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{logTableName}'"); |
| 111 | + var infoSchema = logEvents as InfoSchema[] ?? logEvents.ToArray(); |
| 112 | + |
| 113 | + foreach (var column in standardNames) |
| 114 | + { |
| 115 | + infoSchema.Should().Contain(columns => columns.ColumnName == column); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public void WriteEventToDefaultStandardColumns() |
| 122 | + { |
| 123 | + // arrange |
| 124 | + var loggerConfiguration = new LoggerConfiguration(); |
| 125 | + Log.Logger = loggerConfiguration.WriteTo.MSSqlServer( |
| 126 | + connectionString: DatabaseFixture.LogEventsConnectionString, |
| 127 | + tableName: DatabaseFixture.LogTableName, |
| 128 | + autoCreateSqlTable: true, |
| 129 | + batchPostingLimit: 1, |
| 130 | + period: TimeSpan.FromSeconds(10), |
| 131 | + columnOptions: new ColumnOptions()) |
| 132 | + .CreateLogger(); |
| 133 | + |
| 134 | + var file = File.CreateText("Self.log"); |
| 135 | + Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file)); |
| 136 | + |
| 137 | + // act |
| 138 | + const string loggingInformationMessage = "Logging Information message"; |
| 139 | + Log.Information(loggingInformationMessage); |
| 140 | + |
| 141 | + //Thread.Sleep(50); |
| 142 | + |
| 143 | + Log.CloseAndFlush(); |
| 144 | + |
| 145 | + // assert |
| 146 | + using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString)) |
| 147 | + { |
| 148 | + var logEvents = conn.Query<DefaultStandardLogColumns>($"SELECT Message, Level FROM {DatabaseFixture.LogTableName}"); |
| 149 | + |
| 150 | + logEvents.Should().Contain(e => e.Message.Contains(loggingInformationMessage)); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + public class DefaultStandardLogColumns |
| 156 | + { |
| 157 | + public string Message { get; set; } |
| 158 | + |
| 159 | + public LogEventLevel Level { get; set; } |
| 160 | + } |
| 161 | +} |
0 commit comments