Skip to content

Commit 140461b

Browse files
authored
Merge pull request #50 from colin-young/localdb-tests
Test writing event with custom column names
2 parents 4603cc2 + a274976 commit 140461b

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardColumnNames.cs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data.SqlClient;
4+
using System.Data.SqlTypes;
5+
using System.Diagnostics;
46
using System.IO;
57
using System.Linq;
68
using Dapper;
79
using Xunit;
810
using FluentAssertions;
9-
using Serilog.Events;
1011

1112
namespace Serilog.Sinks.MSSqlServer.Tests
1213
{
@@ -116,6 +117,46 @@ public void TableCreatedWithDefaultNames()
116117
}
117118
}
118119

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+
119160
[Fact]
120161
public void WriteEventToDefaultStandardColumns()
121162
{
@@ -148,11 +189,4 @@ public void WriteEventToDefaultStandardColumns()
148189
}
149190
}
150191
}
151-
152-
public class DefaultStandardLogColumns
153-
{
154-
public string Message { get; set; }
155-
156-
public LogEventLevel Level { get; set; }
157-
}
158192
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace Serilog.Sinks.MSSqlServer.Tests
4+
{
5+
public class CustomStandardLogColumns
6+
{
7+
public int CustomId { get; set; }
8+
public string CustomMessage { get; set; }
9+
public string CustomMessageTemplate { get; set; }
10+
public string CustomLevel { get; set; }
11+
public DateTime CustomTimeStamp { get; set; }
12+
public string CustomException { get; set; }
13+
public string CustomProperties { get; set; }
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Serilog.Events;
2+
3+
namespace Serilog.Sinks.MSSqlServer.Tests
4+
{
5+
public class DefaultStandardLogColumns
6+
{
7+
public string Message { get; set; }
8+
9+
public LogEventLevel Level { get; set; }
10+
}
11+
}

0 commit comments

Comments
 (0)