From 173eebea80e076f0e44f565e6b2f8d191fef5f7b Mon Sep 17 00:00:00 2001 From: Thomas Ardal Date: Sun, 26 May 2024 20:07:13 +0200 Subject: [PATCH] Fixed sonar issues --- .../Serilog.Sinks.ElmahIo.Example/Program.cs | 6 +- .../Sinks/ElmahIo/ElmahIOSink.cs | 4 +- .../Serilog.Sinks.ElmahIo.Tests.csproj | 1 + .../Sinks/ElmahIo/ElmahIOSinkTest.cs | 72 ++++++++++--------- 4 files changed, 45 insertions(+), 38 deletions(-) diff --git a/examples/Serilog.Sinks.ElmahIo.Example/Program.cs b/examples/Serilog.Sinks.ElmahIo.Example/Program.cs index c117362..aa63b57 100644 --- a/examples/Serilog.Sinks.ElmahIo.Example/Program.cs +++ b/examples/Serilog.Sinks.ElmahIo.Example/Program.cs @@ -1,11 +1,11 @@ // Copyright 2014 Serilog Contributors -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/src/Serilog.Sinks.ElmahIo/Sinks/ElmahIo/ElmahIOSink.cs b/src/Serilog.Sinks.ElmahIo/Sinks/ElmahIo/ElmahIOSink.cs index f2f5baa..7968b05 100644 --- a/src/Serilog.Sinks.ElmahIo/Sinks/ElmahIo/ElmahIOSink.cs +++ b/src/Serilog.Sinks.ElmahIo/Sinks/ElmahIo/ElmahIOSink.cs @@ -31,8 +31,8 @@ namespace Serilog.Sinks.ElmahIo /// public class ElmahIoSink : IBatchedLogEventSink { - private static string _assemblyVersion = typeof(ElmahIoSink).GetTypeInfo().Assembly.GetCustomAttribute().Version; - private static string _serilogAssemblyVersion = typeof(Log).GetTypeInfo().Assembly.GetCustomAttribute().Version; + private static readonly string _assemblyVersion = typeof(ElmahIoSink).GetTypeInfo().Assembly.GetCustomAttribute().Version; + private static readonly string _serilogAssemblyVersion = typeof(Log).GetTypeInfo().Assembly.GetCustomAttribute().Version; readonly ElmahIoSinkOptions _options; readonly IElmahioAPI _client; diff --git a/test/Serilog.Sinks.ElmahIo.Tests/Serilog.Sinks.ElmahIo.Tests.csproj b/test/Serilog.Sinks.ElmahIo.Tests/Serilog.Sinks.ElmahIo.Tests.csproj index a42f316..c9c8448 100644 --- a/test/Serilog.Sinks.ElmahIo.Tests/Serilog.Sinks.ElmahIo.Tests.csproj +++ b/test/Serilog.Sinks.ElmahIo.Tests/Serilog.Sinks.ElmahIo.Tests.csproj @@ -4,6 +4,7 @@ net472;net6.0;net7.0;net8.0 Serilog.Sinks.ElmahIo.Tests true + 9.0 diff --git a/test/Serilog.Sinks.ElmahIo.Tests/Sinks/ElmahIo/ElmahIOSinkTest.cs b/test/Serilog.Sinks.ElmahIo.Tests/Sinks/ElmahIo/ElmahIOSinkTest.cs index 76cb5d8..9e9001c 100644 --- a/test/Serilog.Sinks.ElmahIo.Tests/Sinks/ElmahIo/ElmahIOSinkTest.cs +++ b/test/Serilog.Sinks.ElmahIo.Tests/Sinks/ElmahIo/ElmahIOSinkTest.cs @@ -14,7 +14,7 @@ namespace Serilog.Sinks.ElmahIo.Tests { public class ElmahIoSinkTest { - private DateTimeOffset Now = DateTimeOffset.Now; + private readonly DateTimeOffset Now = DateTimeOffset.Now; private IList loggedMessages; private IElmahioAPI clientMock; @@ -35,39 +35,47 @@ public async Task CanEmitCustomFields() { // Arrange var sink = new ElmahIoSink(new ElmahIoSinkOptions(string.Empty, Guid.Empty), clientMock); - var serverVariables = new Dictionary(); - serverVariables.Add(new ScalarValue("serverVariableKey"), new ScalarValue("serverVariableValue")); - var cookies = new Dictionary(); - cookies.Add(new ScalarValue("cookiesKey"), new ScalarValue("cookiesValue")); - var form = new Dictionary(); - form.Add(new ScalarValue("formKey"), new ScalarValue("formValue")); - var queryString = new Dictionary(); - queryString.Add(new ScalarValue("queryStringKey"), new ScalarValue("queryStringValue")); + var serverVariables = new Dictionary + { + { new ScalarValue("serverVariableKey"), new ScalarValue("serverVariableValue") } + }; + var cookies = new Dictionary + { + { new ScalarValue("cookiesKey"), new ScalarValue("cookiesValue") } + }; + var form = new Dictionary + { + { new ScalarValue("formKey"), new ScalarValue("formValue") } + }; + var queryString = new Dictionary + { + { new ScalarValue("queryStringKey"), new ScalarValue("queryStringValue") } + }; // Act await sink.EmitBatchAsync(new List { - new LogEvent( + new( Now, LogEventLevel.Error, null, new MessageTemplate("{type} {hostname} {application} {user} {source} {method} {version} {url} {statusCode}", new List()), new List { - new LogEventProperty("type", new ScalarValue("type")), - new LogEventProperty("hostname", new ScalarValue("hostname")), - new LogEventProperty("application", new ScalarValue("application")), - new LogEventProperty("user", new ScalarValue("user")), - new LogEventProperty("source", new ScalarValue("source")), - new LogEventProperty("method", new ScalarValue("method")), - new LogEventProperty("version", new ScalarValue("version")), - new LogEventProperty("url", new ScalarValue("url")), - new LogEventProperty("statusCode", new ScalarValue(400)), - new LogEventProperty("category", new ScalarValue("category")), - new LogEventProperty("correlationId", new ScalarValue("correlationId")), - new LogEventProperty("serverVariables", new DictionaryValue(serverVariables)), - new LogEventProperty("cookies", new DictionaryValue(cookies)), - new LogEventProperty("form", new DictionaryValue(form)), - new LogEventProperty("queryString", new DictionaryValue(queryString)), + new("type", new ScalarValue("type")), + new("hostname", new ScalarValue("hostname")), + new("application", new ScalarValue("application")), + new("user", new ScalarValue("user")), + new("source", new ScalarValue("source")), + new("method", new ScalarValue("method")), + new("version", new ScalarValue("version")), + new("url", new ScalarValue("url")), + new("statusCode", new ScalarValue(400)), + new("category", new ScalarValue("category")), + new("correlationId", new ScalarValue("correlationId")), + new("serverVariables", new DictionaryValue(serverVariables)), + new("cookies", new DictionaryValue(cookies)), + new("form", new DictionaryValue(form)), + new("queryString", new DictionaryValue(queryString)), } ) }); @@ -108,13 +116,13 @@ public async Task CanEmit() // Act await sink.EmitBatchAsync(new List { - new LogEvent( + new( Now, LogEventLevel.Error, exception, new MessageTemplate("Simple test", new List()), new List { - new LogEventProperty("name", new ScalarValue("value")) + new("name", new ScalarValue("value")) } ) }); @@ -135,9 +143,7 @@ await sink.EmitBatchAsync(new List Assert.That(loggedMessage.Data.Any(d => d.Key == "X-ELMAHIO-EXCEPTIONINSPECTOR")); Assert.That(loggedMessage.Type, Is.EqualTo(typeof(DivideByZeroException).FullName)); Assert.That(loggedMessage.Hostname, Is.EqualTo(Environment.MachineName)); -#if !DOTNETCORE Assert.That(loggedMessage.User, Is.EqualTo("User")); -#endif } [Test] @@ -149,7 +155,7 @@ public async Task CanEmitApplicationFromOptions() // Act await sink.EmitBatchAsync(new List { - new LogEvent(Now, LogEventLevel.Information, null, new MessageTemplate("Hello World", new List()), new List()) + new(Now, LogEventLevel.Information, null, new MessageTemplate("Hello World", new List()), new List()) }); // Assert @@ -168,13 +174,13 @@ public async Task CanEmitCategoryFromSourceContext() // Act await sink.EmitBatchAsync(new List { - new LogEvent( + new( Now, LogEventLevel.Error, null, new MessageTemplate("Test", new List()), new List { - new LogEventProperty("SourceContext", new ScalarValue("category")), + new("SourceContext", new ScalarValue("category")), } ) }); @@ -186,7 +192,7 @@ await sink.EmitBatchAsync(new List Assert.That(loggedMessage.Category, Is.EqualTo("category")); } - private Exception Exception() + private static Exception Exception() { return new Exception("error", new DivideByZeroException()); }