Skip to content

Commit bc00ffa

Browse files
committed
Added code analysis nuget in Serilog.Sinks.MSSqlServer and Serilog.Sinks.MSSqlServer.Tests projects and fixed all problems possible and suppressed some that could not be fixed.
1 parent f3d051d commit bc00ffa

25 files changed

+149
-53
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static class LoggerConfigurationMSSqlServerExtensions
3535
/// <summary>
3636
/// The configuration section name for app.config or web.config configuration files.
3737
/// </summary>
38-
public static string AppConfigSectionName = "MSSqlServerSettingsSection";
38+
public const string AppConfigSectionName = "MSSqlServerSettingsSection";
3939

4040
/// <summary>
4141
/// Adds a sink that writes log events to a table in a MSSqlServer database.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Serilog.Sinks.MSSqlServer;
1919
using System.Configuration;
2020
using Serilog.Formatting;
21+
using System.Diagnostics.CodeAnalysis;
2122

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

@@ -31,6 +32,8 @@ public static class LoggerConfigurationMSSqlServerExtensions
3132
/// <summary>
3233
/// The configuration section name for app.config or web.config configuration files.
3334
/// </summary>
35+
// TODO: Make this non-static. It could cause race condition in unit tests!
36+
[SuppressMessage("Usage", "CA2211:Non-constant fields should not be visible", Justification = "Too hard to change. Accepted for now.")]
3437
public static string AppConfigSectionName = "MSSqlServerSettingsSection";
3538

3639
/// <summary>

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/SetProperty.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23

34
namespace Serilog.Sinks.MSSqlServer
45
{
@@ -22,10 +23,10 @@ public static partial class SetProperty
2223
/// </summary>
2324
public static void IfNotNull<T>(string value, PropertySetter<T> setter)
2425
{
25-
if (value == null) return;
26+
if (value == null || setter == null) return;
2627
try
2728
{
28-
T setting = (T)Convert.ChangeType(value, typeof(T));
29+
T setting = (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
2930
setter(setting);
3031
}
3132
// don't change the property if the conversion fails

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected override ConfigurationElement CreateNewElement()
2828

2929
protected override object GetElementKey(ConfigurationElement element)
3030
{
31-
return ((ColumnConfig)element).ColumnName;
31+
return ((ColumnConfig)element)?.ColumnName;
3232
}
3333
}
3434
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public static partial class SetProperty
1212
/// </summary>
1313
public static void IfProvided<T>(ConfigurationElement element, string propertyName, PropertySetter<T> setter)
1414
{
15+
if (element == null)
16+
{
17+
return;
18+
}
19+
1520
var property = element.ElementInformation.Properties[propertyName];
1621
if (property.ValueOrigin == PropertyValueOrigin.Default) return;
1722
IfNotNull((string)property.Value, setter);
@@ -22,6 +27,11 @@ public static void IfProvided<T>(ConfigurationElement element, string propertyNa
2227
/// </summary>
2328
public static void IfProvidedNotEmpty<T>(ConfigurationElement element, string propertyName, PropertySetter<T> setter)
2429
{
30+
if (element == null)
31+
{
32+
return;
33+
}
34+
2535
var property = element.ElementInformation.Properties[propertyName];
2636
if (property.ValueOrigin == PropertyValueOrigin.Default) return;
2737
IfNotNullOrEmpty((string)property.Value, setter);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected override ConfigurationElement CreateNewElement()
1414

1515
protected override object GetElementKey(ConfigurationElement element)
1616
{
17-
return ((StandardColumnConfig)element).Name;
17+
return ((StandardColumnConfig)element)?.Name;
1818
}
1919
}
2020
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This file is used by Code Analysis to maintain SuppressMessage
2+
// attributes that are applied to this project.
3+
// Project-level suppressions either have no target or are given
4+
// a specific target and scoped to a namespace, type, member, etc.
5+
6+
using System.Diagnostics.CodeAnalysis;
7+
8+
[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")]
9+
[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.MSSqlServerSinkTraits.TryChangeType(System.Object,System.Type,System.Object@)~System.Boolean")]
11+
[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.MSSqlServerSink.EmitBatchAsync(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})~System.Threading.Tasks.Task")]
12+
[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.MSSqlServerSinkTraits.ConvertPropertiesToXmlStructure(System.Collections.Generic.IEnumerable{System.Collections.Generic.KeyValuePair{System.String,Serilog.Events.LogEventPropertyValue}})~System.String")]
13+
[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.MSSqlServerSinkTraits.#ctor(System.String,System.String,System.String,Serilog.Sinks.MSSqlServer.ColumnOptions,System.IFormatProvider,System.Boolean,Serilog.Formatting.ITextFormatter,Serilog.Sinks.MSSqlServer.Platform.ISqlTableCreator)")]
14+
[assembly: SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Serilog core guarantees to call Emit() with non-null logEvent parameter.", Scope = "member", Target = "~M:Serilog.Sinks.MSSqlServer.MSSqlServerAuditSink.Emit(Serilog.Events.LogEvent)")]
15+
[assembly: SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Serilog core guarantees to call EmitBatchAsync() with non-null logEvent parameter.", Scope = "member", Target = "~M:Serilog.Sinks.MSSqlServer.MSSqlServerSink.EmitBatchAsync(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})~System.Threading.Tasks.Task")]
16+
[assembly: SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "Cannot be changed on public classes for backward compatibility reasons.", Scope = "namespaceanddescendants", Target = "Serilog.Sinks.MSSqlServer")]
17+
[assembly: SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "Cannot be changed on public classes for backward compatibility reasons.", Scope = "type", Target = "~T:Serilog.Sinks.MSSqlServer.ColumnOptions")]
18+
[assembly: SuppressMessage("Design", "CA1010:Collections should implement generic interface", Justification = "Cannot be changed on public classes for backward compatibility reasons.", Scope = "namespaceanddescendants", Target = "Serilog.Sinks.MSSqlServer")]
19+
[assembly: SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "Too hard to change. Accepted for now.", Scope = "type", Target = "~T:Serilog.LoggerConfigurationMSSqlServerExtensions")]
20+
[assembly: SuppressMessage("Performance", "CA1822: Member AllowNull does not access instance data and can be marked as static (Shared in VisualBasic)", Justification = "Cannot be changed on public classes for backward compatibility reasons.", Scope = "member", Target = "~P:Serilog.Sinks.MSSqlServer.ColumnOptions.IdColumnOptions.AllowNull")]

src/Serilog.Sinks.MSSqlServer/Serilog.Sinks.MSSqlServer.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
<ItemGroup>
2828
<!-- Whenever these are updated, also update versions in packages.config for net452 -->
29+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
30+
<PrivateAssets>all</PrivateAssets>
31+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
32+
</PackageReference>
2933
<PackageReference Include="Serilog" Version="2.5.0" />
3034
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.1.1" />
3135
</ItemGroup>
@@ -76,7 +80,7 @@
7680
</ItemGroup>
7781

7882
<ItemGroup>
79-
<None Include="Images\serilog-sink-nuget.png" Pack="true" PackagePath=""/>
83+
<None Include="Images\serilog-sink-nuget.png" Pack="true" PackagePath="" />
8084
</ItemGroup>
8185

8286
</Project>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public MSSqlServerAuditSink(
5050
string schemaName = "dbo",
5151
ITextFormatter logEventFormatter = null)
5252
{
53-
columnOptions.FinalizeConfigurationForSinkConstructor();
53+
columnOptions?.FinalizeConfigurationForSinkConstructor();
5454

5555
if (columnOptions != null)
5656
{

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections.Generic;
1717
using System.Data;
1818
using System.Data.SqlClient;
19+
using System.Globalization;
1920
using System.Linq;
2021
using System.Threading.Tasks;
2122
using Serilog.Debugging;
@@ -67,7 +68,7 @@ public MSSqlServerSink(
6768
ITextFormatter logEventFormatter = null)
6869
: base(batchPostingLimit, period)
6970
{
70-
columnOptions.FinalizeConfigurationForSinkConstructor();
71+
columnOptions?.FinalizeConfigurationForSinkConstructor();
7172
_traits = new MSSqlServerSinkTraits(connectionString, tableName, schemaName, columnOptions, formatProvider, autoCreateSqlTable, logEventFormatter);
7273
}
7374

@@ -95,7 +96,7 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
9596
: new SqlBulkCopy(cn, SqlBulkCopyOptions.CheckConstraints | SqlBulkCopyOptions.FireTriggers, null)
9697
)
9798
{
98-
copy.DestinationTableName = string.Format("[{0}].[{1}]", _traits.SchemaName, _traits.TableName);
99+
copy.DestinationTableName = string.Format(CultureInfo.InvariantCulture, "[{0}].[{1}]", _traits.SchemaName, _traits.TableName);
99100
foreach (var column in _traits.EventTable.Columns)
100101
{
101102
var columnName = ((DataColumn)column).ColumnName;

0 commit comments

Comments
 (0)