Skip to content

Commit 6e01950

Browse files
author
Kadluba Christian
committed
* Added parameter in Sink configuration to override ouput formatter for LogEvent column.
* Some refactoring and cleanup.
1 parent 81ae41c commit 6e01950

File tree

15 files changed

+99
-81
lines changed

15 files changed

+99
-81
lines changed

assets/CommonAssemblyInfo.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.

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

Lines changed: 14 additions & 9 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
@@ -50,6 +51,7 @@ public static class LoggerConfigurationMSSqlServerExtensions
5051
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
5152
/// <param name="period">The time to wait between checking for event batches.</param>
5253
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
54+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
5355
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
5456
/// <param name="columnOptions">An externally-modified group of column settings</param>
5557
/// <param name="columnOptionsSection">A config section defining various column settings</param>
@@ -65,14 +67,14 @@ public static LoggerConfiguration MSSqlServer(
6567
int batchPostingLimit = MSSqlServerSink.DefaultBatchPostingLimit,
6668
TimeSpan? period = null,
6769
IFormatProvider formatProvider = null,
70+
ITextFormatter logEventFormatter = null,
6871
bool autoCreateSqlTable = false,
6972
ColumnOptions columnOptions = null,
7073
IConfigurationSection columnOptionsSection = null,
71-
string schemaName = "dbo"
72-
)
74+
string schemaName = "dbo")
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();
@@ -100,6 +102,7 @@ public static LoggerConfiguration MSSqlServer(
100102
batchPostingLimit,
101103
defaultedPeriod,
102104
formatProvider,
105+
logEventFormatter,
103106
autoCreateSqlTable,
104107
colOpts,
105108
schemaName
@@ -116,6 +119,7 @@ public static LoggerConfiguration MSSqlServer(
116119
/// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
117120
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
118121
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
122+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
119123
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
120124
/// <param name="columnOptions">An externally-modified group of column settings</param>
121125
/// <param name="columnOptionsSection">A config section defining various column settings</param>
@@ -129,14 +133,14 @@ public static LoggerConfiguration MSSqlServer(
129133
IConfiguration appConfiguration = null,
130134
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
131135
IFormatProvider formatProvider = null,
136+
ITextFormatter logEventFormatter = null,
132137
bool autoCreateSqlTable = false,
133138
ColumnOptions columnOptions = null,
134139
IConfigurationSection columnOptionsSection = null,
135-
string schemaName = "dbo"
136-
)
140+
string schemaName = "dbo")
137141
{
138-
if(loggerAuditSinkConfiguration == null)
139-
throw new ArgumentNullException("loggerAuditSinkConfiguration");
142+
if (loggerAuditSinkConfiguration == null)
143+
throw new ArgumentNullException(nameof(loggerAuditSinkConfiguration));
140144

141145
var colOpts = columnOptions ?? new ColumnOptions();
142146
var connStr = connectionString;
@@ -161,6 +165,7 @@ public static LoggerConfiguration MSSqlServer(
161165
connStr,
162166
tableName,
163167
formatProvider,
168+
logEventFormatter,
164169
autoCreateSqlTable,
165170
colOpts,
166171
schemaName

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

Lines changed: 15 additions & 11 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

@@ -41,6 +42,7 @@ public static partial class LoggerConfigurationMSSqlServerExtensions
4142
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
4243
/// <param name="period">The time to wait between checking for event batches.</param>
4344
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
45+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
4446
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
4547
/// <param name="columnOptions">An externally-modified group of column settings</param>
4648
/// <param name="columnOptionsSection">A config section defining various column settings</param>
@@ -56,14 +58,14 @@ public static LoggerConfiguration MSSqlServer(
5658
int batchPostingLimit = MSSqlServerSink.DefaultBatchPostingLimit,
5759
TimeSpan? period = null,
5860
IFormatProvider formatProvider = null,
61+
ITextFormatter logEventFormatter = null,
5962
bool autoCreateSqlTable = false,
6063
ColumnOptions columnOptions = null,
6164
IConfigurationSection columnOptionsSection = null,
62-
string schemaName = "dbo"
63-
)
65+
string schemaName = "dbo")
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);
@@ -76,6 +78,7 @@ public static LoggerConfiguration MSSqlServer(
7678
batchPostingLimit,
7779
defaultedPeriod,
7880
formatProvider,
81+
logEventFormatter,
7982
autoCreateSqlTable,
8083
colOpts,
8184
schemaName
@@ -92,6 +95,7 @@ public static LoggerConfiguration MSSqlServer(
9295
/// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
9396
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
9497
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
98+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
9599
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
96100
/// <param name="columnOptions">An externally-modified group of column settings</param>
97101
/// <param name="columnOptionsSection">A config section defining various column settings</param>
@@ -105,14 +109,14 @@ public static LoggerConfiguration MSSqlServer(
105109
IConfiguration appConfiguration = null,
106110
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
107111
IFormatProvider formatProvider = null,
112+
ITextFormatter logEventFormatter = null,
108113
bool autoCreateSqlTable = false,
109114
ColumnOptions columnOptions = null,
110115
IConfigurationSection columnOptionsSection = null,
111-
string schemaName = "dbo"
112-
)
116+
string schemaName = "dbo")
113117
{
114-
if(loggerAuditSinkConfiguration == null)
115-
throw new ArgumentNullException("loggerAuditSinkConfiguration");
118+
if (loggerAuditSinkConfiguration == null)
119+
throw new ArgumentNullException(nameof(loggerAuditSinkConfiguration));
116120

117121
var connectionStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
118122
var colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
@@ -122,10 +126,10 @@ public static LoggerConfiguration MSSqlServer(
122126
connectionString,
123127
tableName,
124128
formatProvider,
129+
logEventFormatter,
125130
autoCreateSqlTable,
126131
columnOptions,
127-
schemaName
128-
),
132+
schemaName),
129133
restrictedToMinimumLevel);
130134
}
131135
}

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

@@ -46,6 +47,7 @@ public static class LoggerConfigurationMSSqlServerExtensions
4647
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
4748
/// <param name="period">The time to wait between checking for event batches.</param>
4849
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
50+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
4951
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
5052
/// <param name="columnOptions"></param>
5153
/// <returns>Logger configuration, allowing configuration to continue.</returns>
@@ -58,12 +60,13 @@ public static LoggerConfiguration MSSqlServer(
5860
int batchPostingLimit = MSSqlServerSink.DefaultBatchPostingLimit,
5961
TimeSpan? period = null,
6062
IFormatProvider formatProvider = null,
63+
ITextFormatter logEventFormatter = null,
6164
bool autoCreateSqlTable = false,
6265
ColumnOptions columnOptions = null,
63-
string schemaName = "dbo"
64-
)
66+
string schemaName = "dbo")
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();
@@ -80,10 +83,10 @@ public static LoggerConfiguration MSSqlServer(
8083
batchPostingLimit,
8184
defaultedPeriod,
8285
formatProvider,
86+
logEventFormatter,
8387
autoCreateSqlTable,
8488
colOpts,
85-
schemaName
86-
),
89+
schemaName),
8790
restrictedToMinimumLevel);
8891
}
8992

@@ -99,20 +102,24 @@ public static LoggerConfiguration MSSqlServer(
99102
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
100103
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
101104
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
105+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
102106
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
103107
/// <param name="columnOptions"></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+
ITextFormatter logEventFormatter = null,
117+
bool autoCreateSqlTable = false,
118+
ColumnOptions columnOptions = null,
119+
string schemaName = "dbo")
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

@@ -126,10 +133,10 @@ public static LoggerConfiguration MSSqlServer(this LoggerAuditSinkConfiguration
126133
connectionString,
127134
tableName,
128135
formatProvider,
136+
logEventFormatter,
129137
autoCreateSqlTable,
130138
colOpts,
131-
schemaName
132-
),
139+
schemaName),
133140
restrictedToMinimumLevel);
134141
}
135142
}

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/System.Configuration/ColumnConfig.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ namespace Serilog.Sinks.MSSqlServer
2323
public class ColumnConfig : ConfigurationElement
2424
{
2525
public ColumnConfig()
26-
{ }
26+
{
27+
}
2728

2829
public ColumnConfig(string columnName, string dataType)
2930
{
@@ -43,7 +44,7 @@ public virtual string ColumnName
4344
public string DataType
4445
{
4546
get { return (string)this["DataType"]; }
46-
set { this["DataType"] = value; }
47+
set { this["DataType"] = value; }
4748
}
4849

4950
[ConfigurationProperty("DataLength")]

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerAuditSink.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 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.
@@ -15,10 +15,10 @@
1515
using Serilog.Core;
1616
using Serilog.Debugging;
1717
using Serilog.Events;
18+
using Serilog.Formatting;
1819
using System;
1920
using System.Data;
2021
using System.Data.SqlClient;
21-
using System.Linq;
2222
using System.Text;
2323

2424
namespace Serilog.Sinks.MSSqlServer
@@ -38,12 +38,14 @@ public class MSSqlServerAuditSink : ILogEventSink, IDisposable
3838
/// <param name="tableName">Name of the table to store the data in.</param>
3939
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
4040
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
41+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
4142
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
4243
/// <param name="columnOptions">Options that pertain to columns</param>
4344
public MSSqlServerAuditSink(
4445
string connectionString,
4546
string tableName,
4647
IFormatProvider formatProvider,
48+
ITextFormatter logEventFormatter = null,
4749
bool autoCreateSqlTable = false,
4850
ColumnOptions columnOptions = null,
4951
string schemaName = "dbo"
@@ -57,7 +59,7 @@ public MSSqlServerAuditSink(
5759
throw new NotSupportedException($"The {nameof(ColumnOptions.DisableTriggers)} option is not supported for auditing.");
5860
}
5961

60-
_traits = new MSSqlServerSinkTraits(connectionString, tableName, schemaName, columnOptions, formatProvider, autoCreateSqlTable);
62+
_traits = new MSSqlServerSinkTraits(connectionString, tableName, schemaName, columnOptions, formatProvider, logEventFormatter, autoCreateSqlTable);
6163

6264
}
6365

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSink.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 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.
@@ -14,13 +14,13 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17-
using System.Collections.ObjectModel;
1817
using System.Data;
1918
using System.Data.SqlClient;
2019
using System.Linq;
2120
using System.Threading.Tasks;
2221
using Serilog.Debugging;
2322
using Serilog.Events;
23+
using Serilog.Formatting;
2424
using Serilog.Sinks.PeriodicBatching;
2525

2626
namespace Serilog.Sinks.MSSqlServer
@@ -52,6 +52,7 @@ public class MSSqlServerSink : PeriodicBatchingSink
5252
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
5353
/// <param name="period">The time to wait between checking for event batches.</param>
5454
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
55+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
5556
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
5657
/// <param name="columnOptions">Options that pertain to columns</param>
5758
public MSSqlServerSink(
@@ -60,14 +61,15 @@ public MSSqlServerSink(
6061
int batchPostingLimit,
6162
TimeSpan period,
6263
IFormatProvider formatProvider,
64+
ITextFormatter logEventFormatter = null,
6365
bool autoCreateSqlTable = false,
6466
ColumnOptions columnOptions = null,
6567
string schemaName = "dbo"
6668
)
6769
: base(batchPostingLimit, period)
6870
{
6971
columnOptions.FinalizeConfigurationForSinkConstructor();
70-
_traits = new MSSqlServerSinkTraits(connectionString, tableName, schemaName, columnOptions, formatProvider, autoCreateSqlTable);
72+
_traits = new MSSqlServerSinkTraits(connectionString, tableName, schemaName, columnOptions, formatProvider, logEventFormatter, autoCreateSqlTable);
7173
}
7274

7375
/// <summary>

0 commit comments

Comments
 (0)