Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test writing event with custom column names #50

Merged
merged 1 commit into from
Sep 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardColumnNames.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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<CustomStandardLogColumns>($"SELECT * FROM {logTableName}");

logEvents.Should().Contain(e => e.CustomMessage.Contains(loggingInformationMessage));
}
}

[Fact]
public void WriteEventToDefaultStandardColumns()
{
Expand Down Expand Up @@ -148,11 +189,4 @@ public void WriteEventToDefaultStandardColumns()
}
}
}

public class DefaultStandardLogColumns
{
public string Message { get; set; }

public LogEventLevel Level { get; set; }
}
}
15 changes: 15 additions & 0 deletions test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardLogColumns.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
11 changes: 11 additions & 0 deletions test/Serilog.Sinks.MSSqlServer.Tests/DefaultStandardLogColumns.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}