Skip to content

Commit 9c373ad

Browse files
authored
Merge pull request #288 from ckadluba/refactor-SinkTraits
Refactoring, tests, fixes
2 parents 1070e30 + 74675e1 commit 9c373ad

33 files changed

+1050
-139
lines changed

src/Serilog.Sinks.MSSqlServer/GlobalSuppressions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
[assembly: SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "Supplying string literals and not using resources is accepted within this project.", Scope = "namespaceanddescendants", Target = "Serilog.Sinks.MSSqlServer")]
99
[assembly: SuppressMessage("Security", "CA2100:Review SQL queries for security vulnerabilities", Justification = "Too hard to change. Accepted for now.", Scope = "namespaceanddescendants", Target = "Serilog.Sinks.MSSqlServer")]
10+
[assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Too hard to change. Accepted for now.", Scope = "member", Target = "~M:Serilog.Sinks.MSSqlServer.SetProperty.IfNotNull``1(System.String,Serilog.Sinks.MSSqlServer.SetProperty.PropertySetter{``0})")]
1011
[assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Too hard to change. Accepted for now.", Scope = "member", Target = "~M:Serilog.Sinks.MSSqlServer.Platform.SqlTableCreator.CreateTable(System.Data.DataTable)")]
1112
[assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Too hard to change. Accepted for now.", Scope = "member", Target = "~M:Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform.SqlBulkBatchWriter.WriteBatch(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent},System.Data.DataTable)~System.Threading.Tasks.Task")]
1213
[assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Too hard to change. Accepted for now.", Scope = "member", Target = "~M:Serilog.Sinks.MSSqlServer.Output.StandardColumnDataGenerator.ConvertPropertiesToXmlStructure(System.Collections.Generic.IEnumerable{System.Collections.Generic.KeyValuePair{System.String,Serilog.Events.LogEventPropertyValue}})~System.String")]

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Dependencies/SinkDependenciesFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal static SinkDependencies Create(
2121

2222
var sqlConnectionFactory =
2323
new SqlConnectionFactory(connectionString,
24+
sinkOptions?.UseAzureManagedIdentity ?? default,
2425
new AzureManagedServiceAuthenticator(
2526
sinkOptions?.UseAzureManagedIdentity ?? default,
2627
sinkOptions.AzureServiceTokenProviderResource));

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/AzureManagedServiceAuthenticator.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System;
2-
#if NET452
3-
using System.Data.SqlClient;
4-
#else
5-
using Microsoft.Data.SqlClient;
6-
#endif
2+
using System.Threading.Tasks;
73
using Microsoft.Azure.Services.AppAuthentication;
84

95
namespace Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform
@@ -26,15 +22,14 @@ public AzureManagedServiceAuthenticator(bool useAzureManagedIdentity, string azu
2622
_azureServiceTokenProvider = new AzureServiceTokenProvider();
2723
}
2824

29-
public void SetAuthenticationToken(SqlConnection sqlConnection)
25+
public Task<string> GetAuthenticationToken()
3026
{
3127
if (!_useAzureManagedIdentity)
3228
{
33-
return;
29+
return Task.FromResult((string)null);
3430
}
3531

36-
sqlConnection.AccessToken = _azureServiceTokenProvider.GetAccessTokenAsync(
37-
_azureServiceTokenProviderResource).GetAwaiter().GetResult();
32+
return _azureServiceTokenProvider.GetAccessTokenAsync(_azureServiceTokenProviderResource);
3833
}
3934
}
4035
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/AzureManagedServiceAuthenticatorStub.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
using System;
2-
#if NET452
3-
using System.Data.SqlClient;
4-
#else
5-
using Microsoft.Data.SqlClient;
6-
#endif
7-
82
using System.Diagnostics.CodeAnalysis;
3+
using System.Threading.Tasks;
94

105
// This is an empty stub implementaion of IAzureManagedServiceAuthenticator for the target frameworks
116
// that don't support Azure Managed Identities (net452, net461, netstandard2.0, netcoreapp2.0).
@@ -29,8 +24,6 @@ public AzureManagedServiceAuthenticator(bool useAzureManagedIdentity, string azu
2924
_azureServiceTokenProviderResource = azureServiceTokenProviderResource;
3025
}
3126

32-
public void SetAuthenticationToken(SqlConnection sqlConnection)
33-
{
34-
}
27+
public Task<string> GetAuthenticationToken() => Task.FromResult((string)null);
3528
}
3629
}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
#if NET452
2-
using System.Data.SqlClient;
3-
#else
4-
using Microsoft.Data.SqlClient;
5-
#endif
1+
using System.Threading.Tasks;
62

73
namespace Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform
84
{
95
internal interface IAzureManagedServiceAuthenticator
106
{
11-
void SetAuthenticationToken(SqlConnection sqlConnection);
7+
Task<string> GetAuthenticationToken();
128
}
139
}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
#if NET452
2-
using System.Data.SqlClient;
3-
#else
4-
using Microsoft.Data.SqlClient;
5-
#endif
1+
using Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform.SqlClient;
62

73
namespace Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform
84
{
95
internal interface ISqlConnectionFactory
106
{
11-
SqlConnection Create();
7+
ISqlConnectionWrapper Create();
128
}
139
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlBulkBatchWriter.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data;
4-
#if NET452
5-
using System.Data.SqlClient;
6-
#else
7-
using Microsoft.Data.SqlClient;
8-
#endif
94
using System.Globalization;
105
using System.Linq;
116
using System.Threading.Tasks;
@@ -39,25 +34,20 @@ public SqlBulkBatchWriter(
3934

4035
public async Task WriteBatch(IEnumerable<LogEvent> events, DataTable dataTable)
4136
{
42-
// Copy the events to the data table
43-
FillDataTable(events, dataTable);
44-
4537
try
4638
{
39+
FillDataTable(events, dataTable);
40+
4741
using (var cn = _sqlConnectionFactory.Create())
4842
{
4943
await cn.OpenAsync().ConfigureAwait(false);
50-
using (var copy = _disableTriggers
51-
? new SqlBulkCopy(cn)
52-
: new SqlBulkCopy(cn, SqlBulkCopyOptions.CheckConstraints | SqlBulkCopyOptions.FireTriggers, null)
53-
)
44+
using (var copy = cn.CreateSqlBulkCopy(_disableTriggers,
45+
string.Format(CultureInfo.InvariantCulture, "[{0}].[{1}]", _schemaName, _tableName)))
5446
{
55-
copy.DestinationTableName = string.Format(CultureInfo.InvariantCulture, "[{0}].[{1}]", _schemaName, _tableName);
5647
foreach (var column in dataTable.Columns)
5748
{
5849
var columnName = ((DataColumn)column).ColumnName;
59-
var mapping = new SqlBulkCopyColumnMapping(columnName, columnName);
60-
copy.ColumnMappings.Add(mapping);
50+
copy.AddSqlBulkCopyColumnMapping(columnName, columnName);
6151
}
6252

6353
await copy.WriteToServerAsync(dataTable).ConfigureAwait(false);
@@ -70,7 +60,6 @@ public async Task WriteBatch(IEnumerable<LogEvent> events, DataTable dataTable)
7060
}
7161
finally
7262
{
73-
// Processed the items, clear for the next run
7463
dataTable.Clear();
7564
}
7665
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Data;
3+
using System.Threading.Tasks;
4+
5+
namespace Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform.SqlClient
6+
{
7+
internal interface ISqlBulkCopyWrapper : IDisposable
8+
{
9+
void AddSqlBulkCopyColumnMapping(string sourceColumn, string destinationColumn);
10+
Task WriteToServerAsync(DataTable table);
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Data;
3+
4+
namespace Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform.SqlClient
5+
{
6+
internal interface ISqlCommandWrapper : IDisposable
7+
{
8+
CommandType CommandType { get; set; }
9+
string CommandText { get; set; }
10+
11+
void AddParameter(string parameterName, object value);
12+
int ExecuteNonQuery();
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform.SqlClient
5+
{
6+
internal interface ISqlConnectionWrapper : IDisposable
7+
{
8+
string ConnectionString { get; }
9+
10+
void Open();
11+
Task OpenAsync();
12+
ISqlCommandWrapper CreateCommand();
13+
ISqlCommandWrapper CreateCommand(string cmdText);
14+
ISqlBulkCopyWrapper CreateSqlBulkCopy(bool disableTriggers, string destinationTableName);
15+
}
16+
}

0 commit comments

Comments
 (0)