Skip to content

Commit

Permalink
Fixed sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasArdal committed May 26, 2024
1 parent 5623421 commit 173eebe
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 38 deletions.
6 changes: 3 additions & 3 deletions examples/Serilog.Sinks.ElmahIo.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/Serilog.Sinks.ElmahIo/Sinks/ElmahIo/ElmahIOSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace Serilog.Sinks.ElmahIo
/// </summary>
public class ElmahIoSink : IBatchedLogEventSink
{
private static string _assemblyVersion = typeof(ElmahIoSink).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
private static string _serilogAssemblyVersion = typeof(Log).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
private static readonly string _assemblyVersion = typeof(ElmahIoSink).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
private static readonly string _serilogAssemblyVersion = typeof(Log).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;

readonly ElmahIoSinkOptions _options;
readonly IElmahioAPI _client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFrameworks>net472;net6.0;net7.0;net8.0</TargetFrameworks>
<AssemblyName>Serilog.Sinks.ElmahIo.Tests</AssemblyName>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<LangVersion>9.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
72 changes: 39 additions & 33 deletions test/Serilog.Sinks.ElmahIo.Tests/Sinks/ElmahIo/ElmahIOSinkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateMessage> loggedMessages;
private IElmahioAPI clientMock;

Expand All @@ -35,39 +35,47 @@ public async Task CanEmitCustomFields()
{
// Arrange
var sink = new ElmahIoSink(new ElmahIoSinkOptions(string.Empty, Guid.Empty), clientMock);
var serverVariables = new Dictionary<ScalarValue, LogEventPropertyValue>();
serverVariables.Add(new ScalarValue("serverVariableKey"), new ScalarValue("serverVariableValue"));
var cookies = new Dictionary<ScalarValue, LogEventPropertyValue>();
cookies.Add(new ScalarValue("cookiesKey"), new ScalarValue("cookiesValue"));
var form = new Dictionary<ScalarValue, LogEventPropertyValue>();
form.Add(new ScalarValue("formKey"), new ScalarValue("formValue"));
var queryString = new Dictionary<ScalarValue, LogEventPropertyValue>();
queryString.Add(new ScalarValue("queryStringKey"), new ScalarValue("queryStringValue"));
var serverVariables = new Dictionary<ScalarValue, LogEventPropertyValue>
{
{ new ScalarValue("serverVariableKey"), new ScalarValue("serverVariableValue") }
};
var cookies = new Dictionary<ScalarValue, LogEventPropertyValue>
{
{ new ScalarValue("cookiesKey"), new ScalarValue("cookiesValue") }
};
var form = new Dictionary<ScalarValue, LogEventPropertyValue>
{
{ new ScalarValue("formKey"), new ScalarValue("formValue") }
};
var queryString = new Dictionary<ScalarValue, LogEventPropertyValue>
{
{ new ScalarValue("queryStringKey"), new ScalarValue("queryStringValue") }
};

// Act
await sink.EmitBatchAsync(new List<LogEvent>
{
new LogEvent(
new(
Now,
LogEventLevel.Error,
null,
new MessageTemplate("{type} {hostname} {application} {user} {source} {method} {version} {url} {statusCode}", new List<MessageTemplateToken>()), new List<LogEventProperty>
{
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)),
}
)
});
Expand Down Expand Up @@ -108,13 +116,13 @@ public async Task CanEmit()
// Act
await sink.EmitBatchAsync(new List<LogEvent>
{
new LogEvent(
new(
Now,
LogEventLevel.Error,
exception,
new MessageTemplate("Simple test", new List<MessageTemplateToken>()), new List<LogEventProperty>
{
new LogEventProperty("name", new ScalarValue("value"))
new("name", new ScalarValue("value"))
}
)
});
Expand All @@ -135,9 +143,7 @@ await sink.EmitBatchAsync(new List<LogEvent>
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]
Expand All @@ -149,7 +155,7 @@ public async Task CanEmitApplicationFromOptions()
// Act
await sink.EmitBatchAsync(new List<LogEvent>
{
new LogEvent(Now, LogEventLevel.Information, null, new MessageTemplate("Hello World", new List<MessageTemplateToken>()), new List<LogEventProperty>())
new(Now, LogEventLevel.Information, null, new MessageTemplate("Hello World", new List<MessageTemplateToken>()), new List<LogEventProperty>())
});

// Assert
Expand All @@ -168,13 +174,13 @@ public async Task CanEmitCategoryFromSourceContext()
// Act
await sink.EmitBatchAsync(new List<LogEvent>
{
new LogEvent(
new(
Now,
LogEventLevel.Error,
null,
new MessageTemplate("Test", new List<MessageTemplateToken>()), new List<LogEventProperty>
{
new LogEventProperty("SourceContext", new ScalarValue("category")),
new("SourceContext", new ScalarValue("category")),
}
)
});
Expand All @@ -186,7 +192,7 @@ await sink.EmitBatchAsync(new List<LogEvent>
Assert.That(loggedMessage.Category, Is.EqualTo("category"));
}

private Exception Exception()
private static Exception Exception()
{
return new Exception("error", new DivideByZeroException());
}
Expand Down

0 comments on commit 173eebe

Please sign in to comment.