Skip to content

Commit 8514922

Browse files
authored
Merge pull request #234 from ckadluba/dev
Overrideable formatter for LogEvent column
2 parents 5bc7f49 + 107ff78 commit 8514922

23 files changed

+345
-190
lines changed

assets/CommonAssemblyInfo.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\Serilog.Sinks.MSSqlServer\Serilog.Sinks.MSSqlServer.csproj" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Serilog.Events;
2+
using Serilog.Formatting;
3+
using System.IO;
4+
using System.Linq;
5+
6+
namespace CustomLogEventFormatterDemo
7+
{
8+
public class FlatLogEventFormatter : ITextFormatter
9+
{
10+
public void Format(LogEvent logEvent, TextWriter output)
11+
{
12+
logEvent.Properties.ToList().ForEach(e =>
13+
{
14+
output.Write($"{e.Key}={e.Value} ");
15+
});
16+
output.WriteLine();
17+
}
18+
}
19+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Serilog;
2+
using Serilog.Sinks.MSSqlServer;
3+
using System;
4+
using System.Threading;
5+
6+
namespace CustomLogEventFormatterDemo
7+
{
8+
public class Program
9+
{
10+
const string _connectionString = "Server=localhost;Database=LogTest;Integrated Security=SSPI;";
11+
const string _schemaName = "dbo";
12+
const string _tableName = "LogEvents";
13+
14+
static void Main()
15+
{
16+
var options = new ColumnOptions();
17+
options.Store.Add(StandardColumn.LogEvent);
18+
var customFormatter = new FlatLogEventFormatter();
19+
Log.Logger = new LoggerConfiguration()
20+
.WriteTo.MSSqlServer(_connectionString,
21+
_tableName,
22+
appConfiguration: null,
23+
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Verbose,
24+
batchPostingLimit: 50,
25+
period: null,
26+
formatProvider: null,
27+
autoCreateSqlTable: true,
28+
columnOptions: options,
29+
columnOptionsSection: null,
30+
schemaName: _schemaName,
31+
logEventFormatter: customFormatter)
32+
.CreateLogger();
33+
34+
try
35+
{
36+
Log.Debug("Getting started");
37+
38+
Log.Information("Hello {Name} from thread {ThreadId}", Environment.GetEnvironmentVariable("USERNAME"), Thread.CurrentThread.ManagedThreadId);
39+
40+
Log.Warning("No coins remain at position {@Position}", new { Lat = 25, Long = 134 });
41+
42+
Fail();
43+
}
44+
catch (DivideByZeroException e)
45+
{
46+
Log.Error(e, "Division by zero");
47+
}
48+
49+
Log.CloseAndFlush();
50+
}
51+
52+
static void Fail()
53+
{
54+
throw new DivideByZeroException();
55+
}
56+
}
57+
}

serilog-sinks-mssqlserver.sln

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26730.16
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29806.167
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{04226074-C72F-42BC-AB02-4D70A7BAE7E1}"
77
EndProject
@@ -19,6 +19,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.MSSqlServer",
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.MSSqlServer.Tests", "test\Serilog.Sinks.MSSqlServer.Tests\Serilog.Sinks.MSSqlServer.Tests.csproj", "{3C2D8E01-5580-426A-BDD9-EC59CD98E618}"
2121
EndProject
22+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{AA346332-5BAF-47F1-B8FB-7600ED61265D}"
23+
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomLogEventFormatterDemo", "sample\CustomLogEventFormatterDemo\CustomLogEventFormatterDemo.csproj", "{873320F1-8F6D-45E2-A853-D321C86793EC}"
25+
EndProject
2226
Global
2327
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2428
Debug|Any CPU = Debug|Any CPU
@@ -33,13 +37,18 @@ Global
3337
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Debug|Any CPU.Build.0 = Debug|Any CPU
3438
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Release|Any CPU.ActiveCfg = Release|Any CPU
3539
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{873320F1-8F6D-45E2-A853-D321C86793EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{873320F1-8F6D-45E2-A853-D321C86793EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{873320F1-8F6D-45E2-A853-D321C86793EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
43+
{873320F1-8F6D-45E2-A853-D321C86793EC}.Release|Any CPU.Build.0 = Release|Any CPU
3644
EndGlobalSection
3745
GlobalSection(SolutionProperties) = preSolution
3846
HideSolutionNode = FALSE
3947
EndGlobalSection
4048
GlobalSection(NestedProjects) = preSolution
4149
{803CD13A-D54B-4CEC-A55F-E22AE3D93B3C} = {04226074-C72F-42BC-AB02-4D70A7BAE7E1}
4250
{3C2D8E01-5580-426A-BDD9-EC59CD98E618} = {F02D6513-6F45-452E-85A0-41A872A2C1F8}
51+
{873320F1-8F6D-45E2-A853-D321C86793EC} = {AA346332-5BAF-47F1-B8FB-7600ED61265D}
4352
EndGlobalSection
4453
GlobalSection(ExtensibilityGlobals) = postSolution
4554
SolutionGuid = {AAA6BF8D-7B53-4A5F-A79A-D1B306383B45}

src/Serilog.Sinks.MSSqlServer/Configuration/Extensions/Hybrid/LoggerConfigurationMSSqlServerExtensions.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 Serilog Contributors
1+
// Copyright 2020 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
using Microsoft.Extensions.Configuration;
2020
using System.Configuration;
2121
using Serilog.Debugging;
22+
using Serilog.Formatting;
2223

2324
// The "Hybrid" configuration system supports both Microsoft.Extensions.Configuration and System.Configuration.
2425
// This is necessary because .NET Framework 4.6.1+ and .NET Core 2.0+ apps support both approaches, whereas the
@@ -54,6 +55,7 @@ public static class LoggerConfigurationMSSqlServerExtensions
5455
/// <param name="columnOptions">An externally-modified group of column settings</param>
5556
/// <param name="columnOptionsSection">A config section defining various column settings</param>
5657
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
58+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
5759
/// <returns>Logger configuration, allowing configuration to continue.</returns>
5860
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
5961
public static LoggerConfiguration MSSqlServer(
@@ -68,11 +70,11 @@ public static LoggerConfiguration MSSqlServer(
6870
bool autoCreateSqlTable = false,
6971
ColumnOptions columnOptions = null,
7072
IConfigurationSection columnOptionsSection = null,
71-
string schemaName = "dbo"
72-
)
73+
string schemaName = "dbo",
74+
ITextFormatter logEventFormatter = null)
7375
{
74-
if(loggerConfiguration == null)
75-
throw new ArgumentNullException("loggerConfiguration");
76+
if (loggerConfiguration == null)
77+
throw new ArgumentNullException(nameof(loggerConfiguration));
7678

7779
var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
7880
var colOpts = columnOptions ?? new ColumnOptions();
@@ -102,8 +104,8 @@ public static LoggerConfiguration MSSqlServer(
102104
formatProvider,
103105
autoCreateSqlTable,
104106
colOpts,
105-
schemaName
106-
),
107+
schemaName,
108+
logEventFormatter),
107109
restrictedToMinimumLevel);
108110
}
109111

@@ -120,6 +122,7 @@ public static LoggerConfiguration MSSqlServer(
120122
/// <param name="columnOptions">An externally-modified group of column settings</param>
121123
/// <param name="columnOptionsSection">A config section defining various column settings</param>
122124
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
125+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
123126
/// <returns>Logger configuration, allowing configuration to continue.</returns>
124127
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
125128
public static LoggerConfiguration MSSqlServer(
@@ -132,11 +135,11 @@ public static LoggerConfiguration MSSqlServer(
132135
bool autoCreateSqlTable = false,
133136
ColumnOptions columnOptions = null,
134137
IConfigurationSection columnOptionsSection = null,
135-
string schemaName = "dbo"
136-
)
138+
string schemaName = "dbo",
139+
ITextFormatter logEventFormatter = null)
137140
{
138-
if(loggerAuditSinkConfiguration == null)
139-
throw new ArgumentNullException("loggerAuditSinkConfiguration");
141+
if (loggerAuditSinkConfiguration == null)
142+
throw new ArgumentNullException(nameof(loggerAuditSinkConfiguration));
140143

141144
var colOpts = columnOptions ?? new ColumnOptions();
142145
var connStr = connectionString;
@@ -163,8 +166,8 @@ public static LoggerConfiguration MSSqlServer(
163166
formatProvider,
164167
autoCreateSqlTable,
165168
colOpts,
166-
schemaName
167-
),
169+
schemaName,
170+
logEventFormatter),
168171
restrictedToMinimumLevel);
169172
}
170173
}

src/Serilog.Sinks.MSSqlServer/Configuration/Extensions/Microsoft.Extensions.Configuration/LoggerConfigurationMSSqlServerExtensions.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 Serilog Contributors
1+
// Copyright 2020 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
using Serilog.Events;
1818
using Serilog.Sinks.MSSqlServer;
1919
using Microsoft.Extensions.Configuration;
20+
using Serilog.Formatting;
2021

2122
// M.E.C. support for .NET Standard 2.0 libraries.
2223

@@ -45,6 +46,7 @@ public static partial class LoggerConfigurationMSSqlServerExtensions
4546
/// <param name="columnOptions">An externally-modified group of column settings</param>
4647
/// <param name="columnOptionsSection">A config section defining various column settings</param>
4748
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
49+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
4850
/// <returns>Logger configuration, allowing configuration to continue.</returns>
4951
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
5052
public static LoggerConfiguration MSSqlServer(
@@ -59,11 +61,11 @@ public static LoggerConfiguration MSSqlServer(
5961
bool autoCreateSqlTable = false,
6062
ColumnOptions columnOptions = null,
6163
IConfigurationSection columnOptionsSection = null,
62-
string schemaName = "dbo"
63-
)
64+
string schemaName = "dbo",
65+
ITextFormatter logEventFormatter = null)
6466
{
65-
if(loggerConfiguration == null)
66-
throw new ArgumentNullException("loggerConfiguration");
67+
if (loggerConfiguration == null)
68+
throw new ArgumentNullException(nameof(loggerConfiguration));
6769

6870
var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
6971
var connectionStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
@@ -78,8 +80,8 @@ public static LoggerConfiguration MSSqlServer(
7880
formatProvider,
7981
autoCreateSqlTable,
8082
colOpts,
81-
schemaName
82-
),
83+
schemaName,
84+
logEventFormatter),
8385
restrictedToMinimumLevel);
8486
}
8587

@@ -96,6 +98,7 @@ public static LoggerConfiguration MSSqlServer(
9698
/// <param name="columnOptions">An externally-modified group of column settings</param>
9799
/// <param name="columnOptionsSection">A config section defining various column settings</param>
98100
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
101+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
99102
/// <returns>Logger configuration, allowing configuration to continue.</returns>
100103
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
101104
public static LoggerConfiguration MSSqlServer(
@@ -108,11 +111,11 @@ public static LoggerConfiguration MSSqlServer(
108111
bool autoCreateSqlTable = false,
109112
ColumnOptions columnOptions = null,
110113
IConfigurationSection columnOptionsSection = null,
111-
string schemaName = "dbo"
112-
)
114+
string schemaName = "dbo",
115+
ITextFormatter logEventFormatter = null)
113116
{
114-
if(loggerAuditSinkConfiguration == null)
115-
throw new ArgumentNullException("loggerAuditSinkConfiguration");
117+
if (loggerAuditSinkConfiguration == null)
118+
throw new ArgumentNullException(nameof(loggerAuditSinkConfiguration));
116119

117120
var connectionStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
118121
var colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
@@ -124,8 +127,8 @@ public static LoggerConfiguration MSSqlServer(
124127
formatProvider,
125128
autoCreateSqlTable,
126129
columnOptions,
127-
schemaName
128-
),
130+
schemaName,
131+
logEventFormatter),
129132
restrictedToMinimumLevel);
130133
}
131134
}

src/Serilog.Sinks.MSSqlServer/Configuration/Extensions/System.Configuration/LoggerConfigurationMSSqlServerExtensions.cs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 Serilog Contributors
1+
// Copyright 2020 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
using Serilog.Events;
1818
using Serilog.Sinks.MSSqlServer;
1919
using System.Configuration;
20+
using Serilog.Formatting;
2021

2122
// System.Configuration support for .NET Framework 4.5.2 libraries and apps.
2223

@@ -48,6 +49,7 @@ public static class LoggerConfigurationMSSqlServerExtensions
4849
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
4950
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
5051
/// <param name="columnOptions"></param>
52+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
5153
/// <returns>Logger configuration, allowing configuration to continue.</returns>
5254
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
5355
public static LoggerConfiguration MSSqlServer(
@@ -60,10 +62,11 @@ public static LoggerConfiguration MSSqlServer(
6062
IFormatProvider formatProvider = null,
6163
bool autoCreateSqlTable = false,
6264
ColumnOptions columnOptions = null,
63-
string schemaName = "dbo"
64-
)
65+
string schemaName = "dbo",
66+
ITextFormatter logEventFormatter = null)
6567
{
66-
if (loggerConfiguration == null) throw new ArgumentNullException("loggerConfiguration");
68+
if (loggerConfiguration == null)
69+
throw new ArgumentNullException(nameof(loggerConfiguration));
6770

6871
var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
6972
var colOpts = columnOptions ?? new ColumnOptions();
@@ -82,8 +85,8 @@ public static LoggerConfiguration MSSqlServer(
8285
formatProvider,
8386
autoCreateSqlTable,
8487
colOpts,
85-
schemaName
86-
),
88+
schemaName,
89+
logEventFormatter),
8790
restrictedToMinimumLevel);
8891
}
8992

@@ -101,18 +104,22 @@ public static LoggerConfiguration MSSqlServer(
101104
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
102105
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
103106
/// <param name="columnOptions"></param>
107+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
104108
/// <returns>Logger configuration, allowing configuration to continue.</returns>
105109
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
106-
public static LoggerConfiguration MSSqlServer(this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration,
107-
string connectionString,
108-
string tableName,
109-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
110-
IFormatProvider formatProvider = null,
111-
bool autoCreateSqlTable = false,
112-
ColumnOptions columnOptions = null,
113-
string schemaName = "dbo")
110+
public static LoggerConfiguration MSSqlServer(
111+
this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration,
112+
string connectionString,
113+
string tableName,
114+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
115+
IFormatProvider formatProvider = null,
116+
bool autoCreateSqlTable = false,
117+
ColumnOptions columnOptions = null,
118+
string schemaName = "dbo",
119+
ITextFormatter logEventFormatter = null)
114120
{
115-
if (loggerAuditSinkConfiguration == null) throw new ArgumentNullException("loggerAuditSinkConfiguration");
121+
if (loggerAuditSinkConfiguration == null)
122+
throw new ArgumentNullException(nameof(loggerAuditSinkConfiguration));
116123

117124
var colOpts = columnOptions ?? new ColumnOptions();
118125

@@ -128,8 +135,8 @@ public static LoggerConfiguration MSSqlServer(this LoggerAuditSinkConfiguration
128135
formatProvider,
129136
autoCreateSqlTable,
130137
colOpts,
131-
schemaName
132-
),
138+
schemaName,
139+
logEventFormatter),
133140
restrictedToMinimumLevel);
134141
}
135142
}

0 commit comments

Comments
 (0)