|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Data.SqlClient;
|
| 4 | +using System.Data.SqlTypes; |
| 5 | +using System.Diagnostics; |
4 | 6 | using System.IO;
|
5 | 7 | using System.Linq;
|
6 | 8 | using Dapper;
|
7 | 9 | using Xunit;
|
8 | 10 | using FluentAssertions;
|
9 |
| -using Serilog.Events; |
10 | 11 |
|
11 | 12 | namespace Serilog.Sinks.MSSqlServer.Tests
|
12 | 13 | {
|
@@ -116,6 +117,46 @@ public void TableCreatedWithDefaultNames()
|
116 | 117 | }
|
117 | 118 | }
|
118 | 119 |
|
| 120 | + [Fact] |
| 121 | + public void WriteEventToCustomStandardColumns() |
| 122 | + { |
| 123 | + // arrange |
| 124 | + var options = new ColumnOptions(); |
| 125 | + |
| 126 | + options.Message.ColumnName = "CustomMessage"; |
| 127 | + options.MessageTemplate.ColumnName = "CustomMessageTemplate"; |
| 128 | + options.Level.ColumnName = "CustomLevel"; |
| 129 | + options.TimeStamp.ColumnName = "CustomTimeStamp"; |
| 130 | + options.Exception.ColumnName = "CustomException"; |
| 131 | + options.Properties.ColumnName = "CustomProperties"; |
| 132 | + options.Id.ColumnName = "CustomId"; |
| 133 | + |
| 134 | + var logTableName = $"{DatabaseFixture.LogTableName}CustomEvent"; |
| 135 | + var loggerConfiguration = new LoggerConfiguration(); |
| 136 | + Log.Logger = loggerConfiguration.WriteTo.MSSqlServer( |
| 137 | + connectionString: DatabaseFixture.LogEventsConnectionString, |
| 138 | + tableName: logTableName, |
| 139 | + autoCreateSqlTable: true, |
| 140 | + columnOptions: options) |
| 141 | + .CreateLogger(); |
| 142 | + |
| 143 | + var file = File.CreateText("CustomColumnsEvent.Self.log"); |
| 144 | + Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file)); |
| 145 | + |
| 146 | + // act |
| 147 | + const string loggingInformationMessage = "Logging Information message"; |
| 148 | + Log.Information(loggingInformationMessage); |
| 149 | + Log.CloseAndFlush(); |
| 150 | + |
| 151 | + // assert |
| 152 | + using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString)) |
| 153 | + { |
| 154 | + var logEvents = conn.Query<CustomStandardLogColumns>($"SELECT * FROM {logTableName}"); |
| 155 | + |
| 156 | + logEvents.Should().Contain(e => e.CustomMessage.Contains(loggingInformationMessage)); |
| 157 | + } |
| 158 | + } |
| 159 | + |
119 | 160 | [Fact]
|
120 | 161 | public void WriteEventToDefaultStandardColumns()
|
121 | 162 | {
|
@@ -148,11 +189,4 @@ public void WriteEventToDefaultStandardColumns()
|
148 | 189 | }
|
149 | 190 | }
|
150 | 191 | }
|
151 |
| - |
152 |
| - public class DefaultStandardLogColumns |
153 |
| - { |
154 |
| - public string Message { get; set; } |
155 |
| - |
156 |
| - public LogEventLevel Level { get; set; } |
157 |
| - } |
158 | 192 | }
|
0 commit comments