Skip to content

Commit

Permalink
Fixed code smells from Sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasArdal committed Jan 8, 2024
1 parent 514e54b commit d1c0d39
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
html {
font-size: 14px;
position: relative;
min-height: 100%;
}

@media (min-width: 768px) {
Expand All @@ -8,11 +10,6 @@ html {
}
}

html {
position: relative;
min-height: 100%;
}

body {
margin-bottom: 60px;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
html {
font-size: 14px;
position: relative;
min-height: 100%;
}

@media (min-width: 768px) {
Expand All @@ -12,11 +14,6 @@ html {
box-shadow: 0 0 0 0.1rem white, 0 0 0 0.25rem #258cfb;
}

html {
position: relative;
min-height: 100%;
}

body {
margin-bottom: 60px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ public class ErrorModel : PageModel

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
public ErrorModel()
{
_logger = logger;
}

public void OnGet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ namespace Elmah.Io.Extensions.Logging.AspNetCore80.SignalR.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;

public IndexModel(ILogger<IndexModel> logger)
public IndexModel()
{
_logger = logger;
}

public void OnGet()
{

// Nothing here
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ namespace Elmah.Io.Extensions.Logging.AspNetCore80.SignalR.Pages
{
public class PrivacyModel : PageModel
{
private readonly ILogger<PrivacyModel> _logger;

public PrivacyModel(ILogger<PrivacyModel> logger)
public PrivacyModel()
{
_logger = logger;
}

public void OnGet()
{
// Nothing here
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
html {
font-size: 14px;
position: relative;
min-height: 100%;
}

@media (min-width: 768px) {
Expand All @@ -12,11 +14,6 @@ html {
box-shadow: 0 0 0 0.1rem white, 0 0 0 0.25rem #258cfb;
}

html {
position: relative;
min-height: 100%;
}

body {
margin-bottom: 60px;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
let connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();

//Disable send button until connection is established
document.getElementById("sendButton").disabled = true;

connection.on("ReceiveMessage", function (user, message) {
var msg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
var encodedMsg = user + " says " + msg;
var li = document.createElement("li");
let msg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
let encodedMsg = user + " says " + msg;
let li = document.createElement("li");
li.textContent = encodedMsg;
document.getElementById("messagesList").appendChild(li);
});
Expand All @@ -20,8 +20,8 @@ connection.start().then(function () {
});

document.getElementById("sendButton").addEventListener("click", function (event) {
var user = document.getElementById("userInput").value;
var message = document.getElementById("messageInput").value;
let user = document.getElementById("userInput").value;
let message = document.getElementById("messageInput").value;
connection.invoke("SendMessage", user, message).catch(function (err) {
return console.error(err.toString());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ public class ErrorModel : PageModel

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
public ErrorModel()
{
_logger = logger;
}

public void OnGet()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
html {
font-size: 14px;
position: relative;
min-height: 100%;
}

@media (min-width: 768px) {
Expand All @@ -12,11 +14,6 @@ html {
box-shadow: 0 0 0 0.1rem white, 0 0 0 0.25rem #258cfb;
}

html {
position: relative;
min-height: 100%;
}

body {
margin-bottom: 60px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
catch
{

// USERS2 exception will be automatically logged
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
catch
{

// USERS2 exception will be automatically logged
}
}

Expand Down
4 changes: 1 addition & 3 deletions samples/Elmah.Io.Extensions.Logging.Function/Function1.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

Expand All @@ -13,7 +11,7 @@ public static void Run([TimerTrigger("0 */5 * * * *", RunOnStartup = true)]Timer
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); // <-- Don't go into elmah.io because of the filter set in Startup.cs
log.LogWarning("This is a warning");
throw new Exception("This is an error");
throw new ApplicationException("This is an error");
}
}
}
6 changes: 1 addition & 5 deletions src/Elmah.Io.Extensions.Logging/ElmahIoLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
// Properties than we cannot map to elmah.io fields, are added to the Data tab.
foreach (var stateProperty in properties)
{
if (stateProperty.Key == OriginalFormatPropertyKey && stateProperty.Value is string value)
{
createMessage.TitleTemplate = value;
continue;
}
if (stateProperty.Key == OriginalFormatPropertyKey && stateProperty.Value is string value) createMessage.TitleTemplate = value;
else if (stateProperty.IsStatusCode(out int? statusCode)) createMessage.StatusCode = statusCode.Value;
else if (stateProperty.IsApplication(out string application)) createMessage.Application = application;
else if (stateProperty.IsSource(out string source)) createMessage.Source = source;
Expand Down
13 changes: 11 additions & 2 deletions src/Elmah.Io.Extensions.Logging/ElmahIoLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,24 @@ public ElmahIoLoggerProvider(string apiKey, Guid logId, ElmahIoProviderOptions o
}

/// <inheritdoc/>
public ILogger CreateLogger(string name)
public ILogger CreateLogger(string categoryName)
{
return new ElmahIoLogger(name, _messageQueue, _options, _scopeProvider);
return new ElmahIoLogger(categoryName, _messageQueue, _options, _scopeProvider);
}

/// <summary>
/// Dispose the internal message queue, trying to process all pending messages.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// This method can be overriden by a subclass to do custom cleanup. Make sure to call: base.Dispose(disposing)
/// </summary>
protected virtual void Dispose(bool disposing)
{
_messageQueue?.Stop();
}
Expand Down
9 changes: 3 additions & 6 deletions src/Elmah.Io.Extensions.Logging/MessageQueueHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ internal class MessageQueueHandler : ICanHandleMessages
private readonly ElmahIoProviderOptions _options;
private IElmahioAPI _elmahIoClient;
private BlockingCollection<CreateMessage> _messages;
private Task _outputTask;
private CancellationTokenSource _cancellationTokenSource;
private readonly List<CreateMessage> _currentBatch = new List<CreateMessage>();
private int _messagesDropped;
Expand All @@ -32,7 +31,7 @@ public void Start()
{
_messages = new BlockingCollection<CreateMessage>(new ConcurrentQueue<CreateMessage>(), _options.BackgroundQueueSize);
_cancellationTokenSource = new CancellationTokenSource();
_outputTask = Task.Run(ProcessLogQueue);
Task.Run(ProcessLogQueue);
}

public void Stop()
Expand All @@ -45,11 +44,9 @@ public void Stop()
ProcessMessages().GetAwaiter().GetResult();
}
}
catch (TaskCanceledException)
{
}
catch (AggregateException ex) when (ex.InnerExceptions.Count == 1 && ex.InnerExceptions[0] is TaskCanceledException)
catch (Exception e) when (e is TaskCanceledException || (e is AggregateException ae && ae.InnerExceptions.Count == 1 && ae.InnerExceptions[0] is TaskCanceledException))
{
// If a TaskCanceledException is thrown while stopping there is not much to do
}

_cancellationTokenSource.Cancel();
Expand Down
3 changes: 0 additions & 3 deletions src/Elmah.Io.Extensions.Logging/SynchronousMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using Elmah.Io.Client;
using System;
using System.Reflection;
using static Elmah.Io.Extensions.Logging.UserAgentHelper;

namespace Elmah.Io.Extensions.Logging
{
internal class SynchronousMessageHandler : ICanHandleMessages
{
internal static string _assemblyVersion = typeof(ElmahIoLogger).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
internal static string _melAssemblyVersion = typeof(Microsoft.Extensions.Logging.ILogger).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
private readonly ElmahIoProviderOptions _options;
private IElmahioAPI _elmahIoClient;

Expand Down
4 changes: 2 additions & 2 deletions src/Elmah.Io.Extensions.Logging/UserAgentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Elmah.Io.Extensions.Logging
{
internal static class UserAgentHelper
{
internal static string _assemblyVersion = typeof(ElmahIoLogger).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
internal static string _melAssemblyVersion = typeof(Microsoft.Extensions.Logging.ILogger).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
private static string _assemblyVersion = typeof(ElmahIoLogger).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
private static string _melAssemblyVersion = typeof(Microsoft.Extensions.Logging.ILogger).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;

internal static string UserAgent()
{
Expand Down

0 comments on commit d1c0d39

Please sign in to comment.