Skip to content

Commit

Permalink
Fixed Sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasArdal committed Jan 10, 2024
1 parent 32a3c4d commit 8e2a6f8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 40 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static Item ToItem<TKey, TValue>(this KeyValuePair<TKey, TValue> keyVal
private static string Trim(string s)
{
if (string.IsNullOrWhiteSpace(s)) return null;
return s.TrimStart(new[] { '\"' }).TrimEnd(new[] { '\"' });
return s.TrimStart('\"').TrimEnd('\"');
}
}
}
23 changes: 11 additions & 12 deletions src/Serilog.Sinks.ElmahIo/Sinks/ElmahIo/ElmahIOSink.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 All @@ -19,7 +19,6 @@
using System.Reflection;
using System.Security.Claims;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Elmah.Io.Client;
using Serilog.Events;
Expand All @@ -32,8 +31,8 @@ namespace Serilog.Sinks.ElmahIo
/// </summary>
public class ElmahIoSink : IBatchedLogEventSink
{
internal static string _assemblyVersion = typeof(ElmahIoSink).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
internal static string _serilogAssemblyVersion = typeof(Log).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
private static string _assemblyVersion = typeof(ElmahIoSink).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
private static string _serilogAssemblyVersion = typeof(Log).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;

readonly ElmahIoSinkOptions _options;
readonly IElmahioAPI _client;
Expand Down Expand Up @@ -162,14 +161,14 @@ private IList<Item> ServerVariables(LogEvent logEvent)
{
var serverVariables = Items(logEvent, "servervariables") ?? new List<Item>();

if (!serverVariables.Any(sv => sv.Key.Equals("User-Agent", StringComparison.OrdinalIgnoreCase)))
if (!serverVariables.Exists(sv => sv.Key.Equals("User-Agent", StringComparison.OrdinalIgnoreCase)))
{
// Look for user agent in properties
var userAgent = String(logEvent, "HttpRequestUserAgent");
if (!string.IsNullOrWhiteSpace(userAgent)) serverVariables.Add(new Item("User-Agent", userAgent));
}

if (!serverVariables.Any(sv => sv.Key.Equals("CLIENT-IP", StringComparison.OrdinalIgnoreCase)
if (!serverVariables.Exists(sv => sv.Key.Equals("CLIENT-IP", StringComparison.OrdinalIgnoreCase)
|| sv.Key.Equals("CLIENT_IP", StringComparison.OrdinalIgnoreCase)
|| sv.Key.Equals("HTTP-CLIENT-IP", StringComparison.OrdinalIgnoreCase)
|| sv.Key.Equals("HTTP_CLIENT_IP", StringComparison.OrdinalIgnoreCase)))
Expand Down Expand Up @@ -206,7 +205,7 @@ private IList<Item> QueryString(LogEvent logEvent)
.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s =>
{
var splitted = s.Split(new[] { '=' });
var splitted = s.Split('=');
var item = new Item();
if (splitted.Length > 0) item.Key = splitted[0];
if (splitted.Length > 1) item.Value = splitted[1];
Expand Down Expand Up @@ -381,11 +380,11 @@ static string String(LogEvent logEvent, string name)

private List<Item> Items(LogEvent logEvent, string keyName)
{
if (logEvent == null || logEvent.Properties == null || logEvent.Properties.Count == 0) return null;
if (!logEvent.Properties.Keys.Any(key => key.Equals(keyName, StringComparison.OrdinalIgnoreCase))) return null;
if (logEvent == null || logEvent.Properties == null || logEvent.Properties.Count == 0) return new List<Item>();
if (!logEvent.Properties.Keys.Any(key => key.Equals(keyName, StringComparison.OrdinalIgnoreCase))) return new List<Item>();

var property = logEvent.Properties.First(prop => prop.Key.Equals(keyName, StringComparison.OrdinalIgnoreCase));
if (!(property.Value is DictionaryValue dictionaryValue)) return null;
if (!(property.Value is DictionaryValue dictionaryValue)) return new List<Item>();

return dictionaryValue
.Elements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ await sink.EmitBatchAsync(new List<LogEvent>
// Assert
Assert.That(loggedMessages != null);
Assert.That(loggedMessages.Count, Is.EqualTo(1));
var loggedMessage = loggedMessages.First();
var loggedMessage = loggedMessages[0];
Assert.That(loggedMessage.Type, Is.EqualTo("type"));
Assert.That(loggedMessage.Hostname, Is.EqualTo("hostname"));
Assert.That(loggedMessage.Application, Is.EqualTo("application"));
Expand Down Expand Up @@ -122,7 +122,7 @@ await sink.EmitBatchAsync(new List<LogEvent>
// Assert
Assert.That(loggedMessages != null);
Assert.That(loggedMessages.Count, Is.EqualTo(1));
var loggedMessage = loggedMessages.First();
var loggedMessage = loggedMessages[0];
Assert.That(loggedMessage.Severity, Is.EqualTo(Severity.Error.ToString()));
Assert.That(loggedMessage.DateTime.HasValue);
Assert.That(loggedMessage.DateTime.Value.DateTime, Is.EqualTo(Now.DateTime.ToUniversalTime()));
Expand Down Expand Up @@ -155,7 +155,7 @@ await sink.EmitBatchAsync(new List<LogEvent>
// Assert
Assert.That(loggedMessages != null);
Assert.That(loggedMessages.Count, Is.EqualTo(1));
var loggedMessage = loggedMessages.First();
var loggedMessage = loggedMessages[0];
Assert.That(loggedMessage.Application, Is.EqualTo("MyApp"));
}

Expand All @@ -182,7 +182,7 @@ await sink.EmitBatchAsync(new List<LogEvent>
// Assert
Assert.That(loggedMessages != null);
Assert.That(loggedMessages.Count, Is.EqualTo(1));
var loggedMessage = loggedMessages.First();
var loggedMessage = loggedMessages[0];
Assert.That(loggedMessage.Category, Is.EqualTo("category"));
}

Expand Down

0 comments on commit 8e2a6f8

Please sign in to comment.