From a2749764207112305817b93a54a051f44c28a12f Mon Sep 17 00:00:00 2001 From: Colin Young Date: Thu, 8 Sep 2016 22:58:46 -0400 Subject: [PATCH] Test writing event with custom column names --- .../CustomStandardColumnNames.cs | 50 ++++++++++++++++--- .../CustomStandardLogColumns.cs | 15 ++++++ .../DefaultStandardLogColumns.cs | 11 ++++ 3 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardLogColumns.cs create mode 100644 test/Serilog.Sinks.MSSqlServer.Tests/DefaultStandardLogColumns.cs diff --git a/test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardColumnNames.cs b/test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardColumnNames.cs index 4b9ec1d3..1d12a8f4 100644 --- a/test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardColumnNames.cs +++ b/test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardColumnNames.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; using System.Data.SqlClient; +using System.Data.SqlTypes; +using System.Diagnostics; using System.IO; using System.Linq; using Dapper; using Xunit; using FluentAssertions; -using Serilog.Events; namespace Serilog.Sinks.MSSqlServer.Tests { @@ -116,6 +117,46 @@ public void TableCreatedWithDefaultNames() } } + [Fact] + public void WriteEventToCustomStandardColumns() + { + // arrange + var options = new ColumnOptions(); + + options.Message.ColumnName = "CustomMessage"; + options.MessageTemplate.ColumnName = "CustomMessageTemplate"; + options.Level.ColumnName = "CustomLevel"; + options.TimeStamp.ColumnName = "CustomTimeStamp"; + options.Exception.ColumnName = "CustomException"; + options.Properties.ColumnName = "CustomProperties"; + options.Id.ColumnName = "CustomId"; + + var logTableName = $"{DatabaseFixture.LogTableName}CustomEvent"; + var loggerConfiguration = new LoggerConfiguration(); + Log.Logger = loggerConfiguration.WriteTo.MSSqlServer( + connectionString: DatabaseFixture.LogEventsConnectionString, + tableName: logTableName, + autoCreateSqlTable: true, + columnOptions: options) + .CreateLogger(); + + var file = File.CreateText("CustomColumnsEvent.Self.log"); + Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file)); + + // act + const string loggingInformationMessage = "Logging Information message"; + Log.Information(loggingInformationMessage); + Log.CloseAndFlush(); + + // assert + using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString)) + { + var logEvents = conn.Query($"SELECT * FROM {logTableName}"); + + logEvents.Should().Contain(e => e.CustomMessage.Contains(loggingInformationMessage)); + } + } + [Fact] public void WriteEventToDefaultStandardColumns() { @@ -148,11 +189,4 @@ public void WriteEventToDefaultStandardColumns() } } } - - public class DefaultStandardLogColumns - { - public string Message { get; set; } - - public LogEventLevel Level { get; set; } - } } diff --git a/test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardLogColumns.cs b/test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardLogColumns.cs new file mode 100644 index 00000000..f96e19bd --- /dev/null +++ b/test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardLogColumns.cs @@ -0,0 +1,15 @@ +using System; + +namespace Serilog.Sinks.MSSqlServer.Tests +{ + public class CustomStandardLogColumns + { + public int CustomId { get; set; } + public string CustomMessage { get; set; } + public string CustomMessageTemplate { get; set; } + public string CustomLevel { get; set; } + public DateTime CustomTimeStamp { get; set; } + public string CustomException { get; set; } + public string CustomProperties { get; set; } + } +} \ No newline at end of file diff --git a/test/Serilog.Sinks.MSSqlServer.Tests/DefaultStandardLogColumns.cs b/test/Serilog.Sinks.MSSqlServer.Tests/DefaultStandardLogColumns.cs new file mode 100644 index 00000000..df416d23 --- /dev/null +++ b/test/Serilog.Sinks.MSSqlServer.Tests/DefaultStandardLogColumns.cs @@ -0,0 +1,11 @@ +using Serilog.Events; + +namespace Serilog.Sinks.MSSqlServer.Tests +{ + public class DefaultStandardLogColumns + { + public string Message { get; set; } + + public LogEventLevel Level { get; set; } + } +} \ No newline at end of file