Skip to content

Commit 060f382

Browse files
committed
* Updated all samples to use new SinkOptions based interface.
* Fixed: TimeSpan parsing for System.Configuration SinkOptionsProvider plus tests
1 parent db77ca9 commit 060f382

File tree

8 files changed

+86
-16
lines changed

8 files changed

+86
-16
lines changed

sample/AppConfigDemo/App.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
88
</startup>
99
<MSSqlServerSettingsSection>
10+
11+
<BatchPostingLimit Value="13" />
12+
<BatchPeriod Value="00:00:15" />
13+
1014
<AddStandardColumns>
1115
<add Name="LogEvent" />
1216
</AddStandardColumns>
1317
<RemoveStandardColumns>
1418
<remove Name="Properties" />
1519
</RemoveStandardColumns>
1620
<TimeStamp ColumnName="TimeStampAlternative" ConvertToUtc="true" />
21+
1722
</MSSqlServerSettingsSection>
1823
<runtime>
1924
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

sample/AppConfigDemo/Program.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Serilog;
44
using Serilog.Events;
55
using Serilog.Sinks.MSSqlServer;
6+
using Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Options;
67

78
namespace AppConfigDemo
89
{
@@ -14,17 +15,34 @@ public static class Program
1415

1516
public static void Main()
1617
{
18+
// Legacy interace - do not use this anymore
19+
//Log.Logger = new LoggerConfiguration().WriteTo
20+
// .MSSqlServer(
21+
// connectionString: _connectionString,
22+
// tableName: _tableName,
23+
// restrictedToMinimumLevel: LogEventLevel.Debug,
24+
// batchPostingLimit: MSSqlServerSink.DefaultBatchPostingLimit,
25+
// period: null,
26+
// formatProvider: null,
27+
// autoCreateSqlTable: true,
28+
// columnOptions: null,
29+
// schemaName: _schemaName,
30+
// logEventFormatter: null)
31+
// .CreateLogger();
32+
33+
// New SinkOptions based interface
1734
Log.Logger = new LoggerConfiguration().WriteTo
1835
.MSSqlServer(
1936
connectionString: _connectionString,
20-
tableName: _tableName,
37+
sinkOptions: new SinkOptions
38+
{
39+
TableName = _tableName,
40+
SchemaName = _schemaName,
41+
AutoCreateSqlTable = true
42+
},
2143
restrictedToMinimumLevel: LogEventLevel.Debug,
22-
batchPostingLimit: MSSqlServerSink.DefaultBatchPostingLimit,
23-
period: null,
2444
formatProvider: null,
25-
autoCreateSqlTable: true,
2645
columnOptions: null,
27-
schemaName: _schemaName,
2846
logEventFormatter: null)
2947
.CreateLogger();
3048

sample/CombinedConfigDemo/Program.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading;
33
using Microsoft.Extensions.Configuration;
44
using Serilog;
5+
using Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Options;
56

67
namespace CombinedConfigDemo
78
{
@@ -20,15 +21,32 @@ public static void Main()
2021
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
2122
.Build();
2223
var columnOptionsSection = configuration.GetSection("Serilog:ColumnOptions");
24+
var sinkOptionsSection = configuration.GetSection("Serilog:SinkOptions");
2325

26+
// Legacy interace - do not use this anymore
27+
//Log.Logger = new LoggerConfiguration()
28+
// .WriteTo.MSSqlServer(
29+
// connectionString: _connectionStringName,
30+
// tableName: _tableName,
31+
// appConfiguration: configuration,
32+
// autoCreateSqlTable: true,
33+
// columnOptionsSection: columnOptionsSection,
34+
// schemaName: _schemaName)
35+
// .CreateLogger();
36+
37+
// New SinkOptions based interface
2438
Log.Logger = new LoggerConfiguration()
2539
.WriteTo.MSSqlServer(
2640
connectionString: _connectionStringName,
27-
tableName: _tableName,
41+
sinkOptions: new SinkOptions
42+
{
43+
TableName = _tableName,
44+
SchemaName = _schemaName,
45+
AutoCreateSqlTable = true
46+
},
47+
sinkOptionsSection: sinkOptionsSection,
2848
appConfiguration: configuration,
29-
autoCreateSqlTable: true,
30-
columnOptionsSection: columnOptionsSection,
31-
schemaName: _schemaName)
49+
columnOptionsSection: columnOptionsSection)
3250
.CreateLogger();
3351

3452
Log.Information("Hello {Name} from thread {ThreadId}", Environment.GetEnvironmentVariable("USERNAME"), Thread.CurrentThread.ManagedThreadId);

sample/CombinedConfigDemo/appsettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
"LogDatabase": "Server=localhost;Database=LogTest;Integrated Security=SSPI;"
1111
},
1212
"Serilog": {
13+
"SinkOptions": {
14+
"batchPostingLimit": 5,
15+
"batchPeriod": "00:00:15"
16+
},
1317
"ColumnOptions": {
1418
"addStandardColumns": [ "LogEvent" ],
1519
"removeStandardColumns": [ "MessageTemplate", "Properties" ],

sample/CustomLogEventFormatterDemo/Program.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading;
33
using Serilog;
44
using Serilog.Sinks.MSSqlServer;
5+
using Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Options;
56

67
namespace CustomLogEventFormatterDemo
78
{
@@ -16,18 +17,37 @@ public static void Main()
1617
var options = new ColumnOptions();
1718
options.Store.Add(StandardColumn.LogEvent);
1819
var customFormatter = new FlatLogEventFormatter();
20+
21+
// Legacy interace - do not use this anymore
22+
//Log.Logger = new LoggerConfiguration()
23+
// .WriteTo.MSSqlServer(_connectionString,
24+
// _tableName,
25+
// appConfiguration: null,
26+
// restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Verbose,
27+
// batchPostingLimit: 50,
28+
// period: null,
29+
// formatProvider: null,
30+
// autoCreateSqlTable: true,
31+
// columnOptions: options,
32+
// columnOptionsSection: null,
33+
// schemaName: _schemaName,
34+
// logEventFormatter: customFormatter)
35+
// .CreateLogger();
36+
37+
// New SinkOptions based interface
1938
Log.Logger = new LoggerConfiguration()
2039
.WriteTo.MSSqlServer(_connectionString,
21-
_tableName,
40+
sinkOptions: new SinkOptions
41+
{
42+
TableName = _tableName,
43+
SchemaName = _schemaName,
44+
AutoCreateSqlTable = true
45+
},
2246
appConfiguration: null,
2347
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Verbose,
24-
batchPostingLimit: 50,
25-
period: null,
2648
formatProvider: null,
27-
autoCreateSqlTable: true,
2849
columnOptions: options,
2950
columnOptionsSection: null,
30-
schemaName: _schemaName,
3151
logEventFormatter: customFormatter)
3252
.CreateLogger();
3353

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using Serilog.Configuration;
34
using Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Options;
45

@@ -26,7 +27,7 @@ private void ReadTableOptions(MSSqlServerConfigurationSection config, SinkOption
2627
private void ReadBatchSettings(MSSqlServerConfigurationSection config, SinkOptions sinkOptions)
2728
{
2829
SetProperty.IfProvided<int>(config.BatchPostingLimit, nameof(config.BatchPostingLimit.Value), val => sinkOptions.BatchPostingLimit = val);
29-
SetProperty.IfProvided<TimeSpan>(config.BatchPeriod, nameof(config.BatchPeriod.Value), val => sinkOptions.BatchPeriod = val);
30+
SetProperty.IfProvided<string>(config.BatchPeriod, nameof(config.BatchPeriod.Value), val => sinkOptions.BatchPeriod = TimeSpan.Parse(val, CultureInfo.InvariantCulture));
3031
}
3132

3233
private void ReadAzureManagedIdentitiesOptions(MSSqlServerConfigurationSection config, SinkOptions sinkOptions)

test/Serilog.Sinks.MSSqlServer.Tests/App.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
<SinkOptionsConfig>
3535
<TableName Value="LogEvents" />
3636
<AutoCreateSqlTable Value="true" />
37+
<BatchPostingLimit Value="13" />
38+
<BatchPeriod Value="00:00:15" />
3739
</SinkOptionsConfig>
3840

3941
</configuration>

test/Serilog.Sinks.MSSqlServer.Tests/Configuration/Extensions/Hybrid/ConfigurationExtensionsTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ private IConfiguration TestConfiguration() =>
136136
{ $"{_columnOptionsSection}:properties:columnName", "CustomProperties" },
137137

138138
{ $"{_sinkOptionsSection}:tableName", DatabaseFixture.LogTableName },
139-
{ $"{_sinkOptionsSection}:autoCreateSqlTable", "true" }
139+
{ $"{_sinkOptionsSection}:autoCreateSqlTable", "true" },
140+
{ $"{_sinkOptionsSection}:batchPostingLimit", "13" },
141+
{ $"{_sinkOptionsSection}:batchPeriod", "00:00:15" }
140142
})
141143
.Build();
142144
}

0 commit comments

Comments
 (0)