From 08a2fdff3805893a861067b34a5ad862b49c4a38 Mon Sep 17 00:00:00 2001 From: Thomas Ardal Date: Thu, 11 Jan 2024 11:33:09 +0100 Subject: [PATCH] Fixed Sonar issues --- .../ElmahIoHeartbeatAttribute.cs | 24 ++++++++----------- .../ElmahIoHeartbeatAttributeTest.cs | 2 +- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/Elmah.Io.Heartbeats.Hangfire/ElmahIoHeartbeatAttribute.cs b/src/Elmah.Io.Heartbeats.Hangfire/ElmahIoHeartbeatAttribute.cs index 23910f8..3c9fd10 100644 --- a/src/Elmah.Io.Heartbeats.Hangfire/ElmahIoHeartbeatAttribute.cs +++ b/src/Elmah.Io.Heartbeats.Hangfire/ElmahIoHeartbeatAttribute.cs @@ -13,8 +13,8 @@ namespace Elmah.Io.Heartbeats.Hangfire /// public class ElmahIoHeartbeatAttribute : JobFilterAttribute, IServerFilter { - internal static string _assemblyVersion = typeof(ElmahIoHeartbeatAttribute).Assembly.GetName().Version.ToString(); - internal static string _hangfireAssemblyVersion = typeof(JobFilterAttribute).Assembly.GetName().Version.ToString(); + private static string _assemblyVersion = typeof(ElmahIoHeartbeatAttribute).Assembly.GetName().Version.ToString(); + private static string _hangfireAssemblyVersion = typeof(JobFilterAttribute).Assembly.GetName().Version.ToString(); private const string StopwatchKeyName = "elmahio-timing"; private readonly Guid logId; @@ -47,32 +47,28 @@ public ElmahIoHeartbeatAttribute(string apiKey, string logId, string heartbeatId /// /// Called by Hangire just before executing the job. /// - public void OnPerforming(PerformingContext context) + public void OnPerforming(PerformingContext filterContext) { var stopwatch = new Stopwatch(); stopwatch.Start(); - context.Items.Add(StopwatchKeyName, stopwatch); + filterContext.Items.Add(StopwatchKeyName, stopwatch); } /// /// Called by Hangfire just after executing the job. /// - public void OnPerformed(PerformedContext context) + public void OnPerformed(PerformedContext filterContext) { long? took = null; - if (context.Items.ContainsKey(StopwatchKeyName)) + if (filterContext.Items.ContainsKey(StopwatchKeyName) && filterContext.Items[StopwatchKeyName] is Stopwatch stopwatch) { - var stopwatch = context.Items[StopwatchKeyName] as Stopwatch; - if (stopwatch != null) - { - stopwatch.Stop(); - took = stopwatch.ElapsedMilliseconds; - } + stopwatch.Stop(); + took = stopwatch.ElapsedMilliseconds; } - if (context.Exception != null) + if (filterContext.Exception != null) { - heartbeats.Unhealthy(logId, heartbeatId, context.Exception.ToString(), took: took); + heartbeats.Unhealthy(logId, heartbeatId, filterContext.Exception.ToString(), took: took); } else { diff --git a/test/Elmah.Io.Heartbeats.Hangfire.Tests/ElmahIoHeartbeatAttributeTest.cs b/test/Elmah.Io.Heartbeats.Hangfire.Tests/ElmahIoHeartbeatAttributeTest.cs index c627414..e748419 100644 --- a/test/Elmah.Io.Heartbeats.Hangfire.Tests/ElmahIoHeartbeatAttributeTest.cs +++ b/test/Elmah.Io.Heartbeats.Hangfire.Tests/ElmahIoHeartbeatAttributeTest.cs @@ -53,7 +53,7 @@ public void CanReportUnhealthyOnException() heartbeatsClientMock.DidNotReceive().Healthy(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); } - private HangfireServer.PerformedContext PerformedContext(Exception exception) + private static HangfireServer.PerformedContext PerformedContext(Exception exception) { return new HangfireServer.PerformedContext( new HangfireServer.PerformContext(