Skip to content

Commit a55daf1

Browse files
committed
Code analysis fixes
* Activated CA in tests project. * Use InvariantCulture on all interpolated strings.
1 parent bf60005 commit a55daf1

25 files changed

+190
-124
lines changed

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/Microsoft.Extensions.Configuration/MicrosoftExtensionsColumnOptionsProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.ObjectModel;
44
using System.Linq;
55
using Microsoft.Extensions.Configuration;
6+
using static System.FormattableString;
67

78
namespace Serilog.Sinks.MSSqlServer.Configuration
89
{
@@ -182,7 +183,7 @@ private static void ReadMiscColumnOptions(IConfigurationSection config, ColumnOp
182183
}
183184

184185
if (columnOptions.PrimaryKey == null)
185-
throw new ArgumentException($"Could not match the configured primary key column name \"{pkName}\" with a data column in the table.");
186+
throw new ArgumentException(Invariant($"Could not match the configured primary key column name \"{pkName}\" with a data column in the table."));
186187
}
187188
}
188189
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using System;
1616
using System.Configuration;
17+
using static System.FormattableString;
1718

1819
// Disable XML comment warnings for internal config classes which are required to have public members
1920
#pragma warning disable 1591
@@ -89,7 +90,8 @@ internal SqlColumn AsSqlColumn()
8990
SetProperty.IfProvided<int>(this, nameof(DataLength), (val) => sqlColumn.DataLength = val);
9091

9192
if (sqlColumn.DataLength == 0 && SqlDataTypes.DataLengthRequired.Contains(sqlColumn.DataType))
92-
throw new ArgumentException($"SQL column {ColumnName} of data type {sqlColumn.DataType} requires a non-zero DataLength property.");
93+
throw new ArgumentException(Invariant(
94+
$"SQL column {ColumnName} of data type {sqlColumn.DataType} requires a non-zero DataLength property."));
9395

9496
SetProperty.IfProvided<bool>(this, nameof(AllowNull), (val) => sqlColumn.AllowNull = val);
9597

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.ObjectModel;
33
using Serilog.Configuration;
4+
using static System.FormattableString;
45

56
namespace Serilog.Sinks.MSSqlServer.Configuration
67
{
@@ -158,7 +159,8 @@ private static void ReadMiscColumnOptions(MSSqlServerConfigurationSection config
158159
}
159160

160161
if (columnOptions.PrimaryKey == null)
161-
throw new ArgumentException($"Could not match the configured primary key column name \"{pkName}\" with a data column in the table.");
162+
throw new ArgumentException(Invariant(
163+
$"Could not match the configured primary key column name \"{pkName}\" with a data column in the table."));
162164
}
163165
}
164166
}

src/Serilog.Sinks.MSSqlServer/Extensions/StringExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using static System.FormattableString;
2+
13
namespace Serilog.Sinks.MSSqlServer.Extensions
24
{
35
internal static class StringExtensions
@@ -9,7 +11,7 @@ public static string Truncate(this string value, int maxLength, string suffix)
911
if (maxLength <= suffixLength) return string.Empty;
1012

1113
var correctedMaxLength = maxLength - suffixLength;
12-
return value.Length <= maxLength ? value : $"{value.Substring(0, correctedMaxLength)}{suffix}";
14+
return value.Length <= maxLength ? value : Invariant($"{value.Substring(0, correctedMaxLength)}{suffix}");
1315
}
1416
}
1517
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/ColumnOptions/FinalizeConfigurationForSinkConstructor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.ObjectModel;
33
using Serilog.Debugging;
4+
using static System.FormattableString;
45

56
namespace Serilog.Sinks.MSSqlServer
67
{
@@ -58,7 +59,7 @@ internal void FinalizeConfigurationForSinkConstructor()
5859
throw new ArgumentException("All custom columns must have a valid ColumnName property.");
5960

6061
if (col.DataType == SqlDataTypes.NotSupported)
61-
throw new ArgumentException($"Column \"{col.ColumnName}\" specified an invalid or unsupported SQL column type.");
62+
throw new ArgumentException(Invariant($"Column \"{col.ColumnName}\" specified an invalid or unsupported SQL column type."));
6263

6364
if (ClusteredColumnstoreIndex)
6465
ColumnstoreCompatibilityCheck(col);
@@ -78,7 +79,7 @@ internal void FinalizeConfigurationForSinkConstructor()
7879
private static void ColumnstoreCompatibilityCheck(SqlColumn column)
7980
{
8081
if (!SqlDataTypes.ColumnstoreCompatible.Contains(column.DataType))
81-
throw new ArgumentException($"Columnstore indexes do not support data type \"{column.DataType}\" declared for column \"{column.ColumnName}\".");
82+
throw new ArgumentException(Invariant($"Columnstore indexes do not support data type \"{column.DataType}\" declared for column \"{column.ColumnName}\"."));
8283

8384
if (column.DataLength == -1 && SqlDataTypes.DataLengthRequired.Contains(column.DataType))
8485
SelfLog.WriteLine("Warning: SQL2017 or newer required to use columnstore index with MAX length column \"{0}\".", column.ColumnName);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Serilog.Formatting;
1919
using Serilog.Sinks.MSSqlServer.Dependencies;
2020
using Serilog.Sinks.MSSqlServer.Platform;
21+
using static System.FormattableString;
2122

2223
namespace Serilog.Sinks.MSSqlServer
2324
{
@@ -123,7 +124,7 @@ private static void ValidateParameters(MSSqlServerSinkOptions sinkOptions, Colum
123124
}
124125

125126
if (columnOptions.DisableTriggers)
126-
throw new NotSupportedException($"The {nameof(ColumnOptions.DisableTriggers)} option is not supported for auditing.");
127+
throw new NotSupportedException(Invariant($"The {nameof(ColumnOptions.DisableTriggers)} option is not supported for auditing."));
127128
}
128129

129130
private static void CheckSinkDependencies(SinkDependencies sinkDependencies)
@@ -135,17 +136,17 @@ private static void CheckSinkDependencies(SinkDependencies sinkDependencies)
135136

136137
if (sinkDependencies.DataTableCreator == null)
137138
{
138-
throw new InvalidOperationException($"DataTableCreator is not initialized!");
139+
throw new InvalidOperationException("DataTableCreator is not initialized!");
139140
}
140141

141142
if (sinkDependencies.SqlTableCreator == null)
142143
{
143-
throw new InvalidOperationException($"SqlTableCreator is not initialized!");
144+
throw new InvalidOperationException("SqlTableCreator is not initialized!");
144145
}
145146

146147
if (sinkDependencies.SqlLogEventWriter == null)
147148
{
148-
throw new InvalidOperationException($"SqlLogEventWriter is not initialized!");
149+
throw new InvalidOperationException("SqlLogEventWriter is not initialized!");
149150
}
150151
}
151152

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,17 @@ private static void CheckSinkDependencies(SinkDependencies sinkDependencies)
175175

176176
if (sinkDependencies.DataTableCreator == null)
177177
{
178-
throw new InvalidOperationException($"DataTableCreator is not initialized!");
178+
throw new InvalidOperationException("DataTableCreator is not initialized!");
179179
}
180180

181181
if (sinkDependencies.SqlTableCreator == null)
182182
{
183-
throw new InvalidOperationException($"SqlTableCreator is not initialized!");
183+
throw new InvalidOperationException("SqlTableCreator is not initialized!");
184184
}
185185

186186
if (sinkDependencies.SqlBulkBatchWriter == null)
187187
{
188-
throw new InvalidOperationException($"SqlBulkBatchWriter is not initialized!");
188+
throw new InvalidOperationException("SqlBulkBatchWriter is not initialized!");
189189
}
190190
}
191191

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlColumn.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Data;
3+
using static System.FormattableString;
34

45
namespace Serilog.Sinks.MSSqlServer
56
{
@@ -38,13 +39,13 @@ public SqlColumn(DataColumn dataColumn)
3839
AllowNull = dataColumn.AllowDBNull;
3940

4041
if (!SqlDataTypes.ReverseTypeMap.ContainsKey(dataColumn.DataType))
41-
throw new ArgumentException($".NET type {dataColumn.DataType} does not map to a supported SQL column data type.");
42+
throw new ArgumentException(Invariant($".NET type {dataColumn.DataType} does not map to a supported SQL column data type."));
4243

4344
DataType = SqlDataTypes.ReverseTypeMap[dataColumn.DataType];
4445
DataLength = dataColumn.MaxLength;
4546

4647
if (DataLength == 0 && SqlDataTypes.DataLengthRequired.Contains(DataType))
47-
throw new ArgumentException($".NET type {dataColumn.DataType} maps to a SQL column data type requiring a non-zero DataLength property.");
48+
throw new ArgumentException(Invariant($".NET type {dataColumn.DataType} maps to a SQL column data type requiring a non-zero DataLength property."));
4849
}
4950

5051
/// <summary>
@@ -74,7 +75,7 @@ public SqlDbType DataType
7475
set
7576
{
7677
if (!SqlDataTypes.SystemTypeMap.ContainsKey(value))
77-
throw new ArgumentException($"SQL column data type {value} is not supported by this sink.");
78+
throw new ArgumentException(Invariant($"SQL column data type {value} is not supported by this sink."));
7879
_dataType = value;
7980
}
8081
}
@@ -129,7 +130,7 @@ internal virtual DataColumn AsDataColumn()
129130
if (SqlDataTypes.DataLengthRequired.Contains(DataType))
130131
{
131132
if (DataLength == 0)
132-
throw new ArgumentException($"Column \"{ColumnName}\" is of type {DataType.ToString().ToUpperInvariant()} which requires a non-zero DataLength.");
133+
throw new ArgumentException(Invariant($"Column \"{ColumnName}\" is of type {DataType.ToString().ToUpperInvariant()} which requires a non-zero DataLength."));
133134

134135
dataColumn.MaxLength = DataLength;
135136
}
@@ -145,7 +146,7 @@ internal virtual DataColumn AsDataColumn()
145146
internal void SetDataTypeFromConfigString(string requestedSqlType)
146147
{
147148
if (!SqlDataTypes.TryParseIfSupported(requestedSqlType, out SqlDbType sqlType))
148-
throw new ArgumentException($"SQL column data type {requestedSqlType} is not recognized or not supported by this sink.");
149+
throw new ArgumentException(Invariant($"SQL column data type {requestedSqlType} is not recognized or not supported by this sink."));
149150

150151
DataType = sqlType;
151152
}

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

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using Microsoft.Extensions.Configuration;
45
using Serilog.Sinks.MSSqlServer.Tests.TestUtils;
56
using Xunit;
67
using Xunit.Abstractions;
8+
using static System.FormattableString;
79

810
namespace Serilog.Sinks.MSSqlServer.Tests.Configuration.Extensions.Hybrid
911
{
@@ -31,7 +33,8 @@ public void ConnectionStringByNameFromConfigLegacyInterface()
3133
connectionString: _connectionStringName,
3234
tableName: DatabaseFixture.LogTableName,
3335
autoCreateSqlTable: true,
34-
appConfiguration: appConfig)
36+
appConfiguration: appConfig,
37+
formatProvider: CultureInfo.InvariantCulture)
3538
.CreateLogger();
3639

3740
// should not throw
@@ -52,7 +55,8 @@ public void ConnectionStringByNameFromConfigSinkOptionsInterface()
5255
TableName = DatabaseFixture.LogTableName,
5356
AutoCreateSqlTable = true
5457
},
55-
appConfiguration: appConfig)
58+
appConfiguration: appConfig,
59+
formatProvider: CultureInfo.InvariantCulture)
5660
.CreateLogger();
5761

5862
// should not throw
@@ -68,17 +72,18 @@ public void ColumnOptionsFromConfigSectionLegacyInterface()
6872
var standardNames = new List<string> { "CustomMessage", "CustomMessageTemplate", "CustomLevel", "CustomTimeStamp",
6973
"CustomException", "CustomProperties", _additionalColumn1Name };
7074
var columnOptionsSection = TestConfiguration().GetSection(_columnOptionsSection);
71-
var messageTemplate = $"Hello {{{_additionalColumn1PropertyName}}}!";
75+
var messageTemplate = Invariant($"Hello {{{_additionalColumn1PropertyName}}}!");
7276
var propertyValue = 2;
73-
var expectedMessage = $"Hello {propertyValue}!";
77+
var expectedMessage = Invariant($"Hello {propertyValue}!");
7478

7579
// Act
7680
var loggerConfiguration = new LoggerConfiguration();
7781
Log.Logger = loggerConfiguration.WriteTo.MSSqlServer(
7882
connectionString: DatabaseFixture.LogEventsConnectionString,
7983
tableName: DatabaseFixture.LogTableName,
8084
autoCreateSqlTable: true,
81-
columnOptionsSection: columnOptionsSection)
85+
columnOptionsSection: columnOptionsSection,
86+
formatProvider: CultureInfo.InvariantCulture)
8287
.CreateLogger();
8388
Log.Information(messageTemplate, propertyValue);
8489
Log.CloseAndFlush();
@@ -96,9 +101,9 @@ public void ColumnOptionsFromConfigSectionSinkOptionsInterface()
96101
var standardNames = new List<string> { "CustomMessage", "CustomMessageTemplate", "CustomLevel", "CustomTimeStamp",
97102
"CustomException", "CustomProperties", _additionalColumn1Name };
98103
var columnOptionsSection = TestConfiguration().GetSection(_columnOptionsSection);
99-
var messageTemplate = $"Hello {{{_additionalColumn1PropertyName}}}!";
104+
var messageTemplate = Invariant($"Hello {{{_additionalColumn1PropertyName}}}!");
100105
var propertyValue = 2;
101-
var expectedMessage = $"Hello {propertyValue}!";
106+
var expectedMessage = Invariant($"Hello {propertyValue}!");
102107

103108
// Act
104109
var loggerConfiguration = new LoggerConfiguration();
@@ -109,7 +114,8 @@ public void ColumnOptionsFromConfigSectionSinkOptionsInterface()
109114
TableName = DatabaseFixture.LogTableName,
110115
AutoCreateSqlTable = true
111116
},
112-
columnOptionsSection: columnOptionsSection)
117+
columnOptionsSection: columnOptionsSection,
118+
formatProvider: CultureInfo.InvariantCulture)
113119
.CreateLogger();
114120
Log.Information(messageTemplate, propertyValue);
115121
Log.CloseAndFlush();
@@ -133,7 +139,8 @@ public void SinkOptionsFromConfigSection()
133139
Log.Logger = loggerConfiguration.WriteTo.MSSqlServer(
134140
connectionString: DatabaseFixture.LogEventsConnectionString,
135141
sinkOptionsSection: sinkOptionsSection,
136-
columnOptionsSection: columnOptionsSection)
142+
columnOptionsSection: columnOptionsSection,
143+
formatProvider: CultureInfo.InvariantCulture)
137144
.CreateLogger();
138145
Log.CloseAndFlush();
139146

@@ -145,22 +152,22 @@ private static IConfiguration TestConfiguration() =>
145152
new ConfigurationBuilder()
146153
.AddInMemoryCollection(new Dictionary<string, string>
147154
{
148-
{ $"ConnectionStrings:{_connectionStringName}", DatabaseFixture.LogEventsConnectionString },
149-
150-
{ $"{_columnOptionsSection}:message:columnName", "CustomMessage" },
151-
{ $"{_columnOptionsSection}:messageTemplate:columnName", "CustomMessageTemplate" },
152-
{ $"{_columnOptionsSection}:level:columnName", "CustomLevel" },
153-
{ $"{_columnOptionsSection}:timeStamp:columnName", "CustomTimeStamp" },
154-
{ $"{_columnOptionsSection}:exception:columnName", "CustomException" },
155-
{ $"{_columnOptionsSection}:properties:columnName", "CustomProperties" },
156-
{ $"{_columnOptionsSection}:additionalColumns:0:columnName", _additionalColumn1Name },
157-
{ $"{_columnOptionsSection}:additionalColumns:0:propertyName", _additionalColumn1PropertyName },
158-
{ $"{_columnOptionsSection}:additionalColumns:0:dataType", "8" },
159-
160-
{ $"{_sinkOptionsSection}:tableName", DatabaseFixture.LogTableName },
161-
{ $"{_sinkOptionsSection}:autoCreateSqlTable", "true" },
162-
{ $"{_sinkOptionsSection}:batchPostingLimit", "13" },
163-
{ $"{_sinkOptionsSection}:batchPeriod", "00:00:15" }
155+
{ Invariant($"ConnectionStrings:{_connectionStringName}"), DatabaseFixture.LogEventsConnectionString },
156+
157+
{ Invariant($"{_columnOptionsSection}:message:columnName"), "CustomMessage" },
158+
{ Invariant($"{_columnOptionsSection}:messageTemplate:columnName"), "CustomMessageTemplate" },
159+
{ Invariant($"{_columnOptionsSection}:level:columnName"), "CustomLevel" },
160+
{ Invariant($"{_columnOptionsSection}:timeStamp:columnName"), "CustomTimeStamp" },
161+
{ Invariant($"{_columnOptionsSection}:exception:columnName"), "CustomException" },
162+
{ Invariant($"{_columnOptionsSection}:properties:columnName"), "CustomProperties" },
163+
{ Invariant($"{_columnOptionsSection}:additionalColumns:0:columnName"), _additionalColumn1Name },
164+
{ Invariant($"{_columnOptionsSection}:additionalColumns:0:propertyName"), _additionalColumn1PropertyName },
165+
{ Invariant($"{_columnOptionsSection}:additionalColumns:0:dataType"), "8" },
166+
167+
{ Invariant($"{_sinkOptionsSection}:tableName"), DatabaseFixture.LogTableName },
168+
{ Invariant($"{_sinkOptionsSection}:autoCreateSqlTable"), "true" },
169+
{ Invariant($"{_sinkOptionsSection}:batchPostingLimit"), "13" },
170+
{ Invariant($"{_sinkOptionsSection}:batchPeriod"), "00:00:15" }
164171
})
165172
.Build();
166173
}

test/Serilog.Sinks.MSSqlServer.Tests/Misc/AdditionalPropertiesTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Data;
3+
using System.Globalization;
34
using Serilog.Sinks.MSSqlServer.Tests.TestUtils;
45
using Xunit;
56
using Xunit.Abstractions;
@@ -53,7 +54,8 @@ public void WritesLogEventWithColumnNamedProperties()
5354
TableName = DatabaseFixture.LogTableName,
5455
AutoCreateSqlTable = true
5556
},
56-
columnOptions: columnOptions)
57+
columnOptions: columnOptions,
58+
formatProvider: CultureInfo.InvariantCulture)
5759
.CreateLogger();
5860
Log.Information(messageTemplate, property1Value, property2Value);
5961
Log.CloseAndFlush();
@@ -109,7 +111,8 @@ public void WritesLogEventWithCustomNamedProperties()
109111
TableName = DatabaseFixture.LogTableName,
110112
AutoCreateSqlTable = true
111113
},
112-
columnOptions: columnOptions)
114+
columnOptions: columnOptions,
115+
formatProvider: CultureInfo.InvariantCulture)
113116
.CreateLogger();
114117
Log.Information(messageTemplate, property1Value, property2Value);
115118
Log.CloseAndFlush();

0 commit comments

Comments
 (0)