Skip to content

Commit 67ecd09

Browse files
committed
fixed warnings and updated logging templates
- parameter placeholders in logging templates need to have proper names https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/quality-rules/ca2253 - other minor fixes and cleanups
1 parent 9edda3d commit 67ecd09

8 files changed

Lines changed: 29 additions & 34 deletions

File tree

src/PodLoad.Client/CpuLoadSimulator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public int Percentage
1616
if(_percentage == value)
1717
return;
1818

19-
if (value < 0 || value > 100)
19+
if (value is < 0 or > 100)
2020
throw new ArgumentOutOfRangeException(nameof(Percentage), "Percentage must be between 0 and 100");
2121

2222
_percentage = value;
@@ -39,11 +39,11 @@ private void SimulateCpuLoad()
3939
return;
4040

4141
var internalToken = _internalCancellationTokenSource.Token;
42-
for (int i = 0; i < Environment.ProcessorCount; i++)
42+
for (var i = 0; i < Environment.ProcessorCount; i++)
4343
{
4444
var thread = new Thread(() =>
4545
{
46-
Stopwatch watch = new Stopwatch();
46+
var watch = new Stopwatch();
4747
watch.Start();
4848
while (!_externalStoppingToken.IsCancellationRequested && !internalToken.IsCancellationRequested)
4949
{

src/PodLoad.Client/Worker.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,26 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2626
memoryKeeper.Allocate(memoryToAllocate * 1024 * 1024);
2727

2828

29-
logger.LogInformation("Started service on host: {0} with IP: {1}. Client ID: {2}", hostName, hostIpAddress, clientId);
30-
logger.LogInformation("Using default params - Memory size: {0} MB, CPU Load: {1}%", memoryToAllocate, percentage);
29+
logger.LogInformation("Started service on host: {hostName} with IP: {ipAddress}. Client ID: {clientId}", hostName, hostIpAddress, clientId);
30+
logger.LogInformation("Using default params - Memory size: {memorySize} MB, CPU Load: {cpuLoad}%", memoryToAllocate, percentage);
3131

3232
// Build report URI and prepare http client
33-
var uriBuilder = new UriBuilder() { Scheme = "http", Host = serverAddress, Port = serverPort, Path = reportEndpoint };
33+
var uriBuilder = new UriBuilder { Scheme = "http", Host = serverAddress, Port = serverPort, Path = reportEndpoint };
3434
using var client = new HttpClient();
3535
var reportUri = uriBuilder.Uri;
3636

37-
logger.LogInformation("Reports endpoint set to {0}", reportUri);
37+
logger.LogInformation("Reports endpoint set to {reportsUrl}", reportUri);
3838

3939
while (!stoppingToken.IsCancellationRequested)
4040
{
4141
TimeSpan requestTimeOut;
4242
try
4343
{
44-
using var content = JsonContent.Create(new ClientReportRequest()
44+
using var content = JsonContent.Create(new ClientReportRequest
4545
{
4646
ClientId = clientId,
4747
ClientHostName = hostName,
48-
ClientIpAddress = hostIpAddress.ToString(),
48+
ClientIpAddress = hostIpAddress,
4949
Percentage = percentage,
5050
MemoryAllocated = memoryToAllocate
5151
});
@@ -62,14 +62,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
6262
{
6363
percentage = clientReportResponse.DesiredPercentage;
6464
cpuLoadSimulator.Percentage = percentage;
65-
logger.LogInformation("New CPU load: {0}%", percentage);
65+
logger.LogInformation("New CPU load: {cpuLoad}%", percentage);
6666
}
6767

6868
if (memoryToAllocate != clientReportResponse.DesiredMemoryAllocated)
6969
{
7070
memoryToAllocate = clientReportResponse.DesiredMemoryAllocated;
7171
memoryKeeper.Allocate(memoryToAllocate * 1024 * 1024);
72-
logger.LogInformation("New memory size: {0} MB", memoryToAllocate);;
72+
logger.LogInformation("New memory size: {memorySize} MB", memoryToAllocate);
7373
}
7474

7575

src/PodLoad.Common/Contracts/ClientReportRequest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Net;
32

43
namespace PodLoad.Common.Contracts
54
{

src/PodLoad.Common/Services/IHostInfoService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Net;
2-
3-
namespace PodLoad.Common.Services
1+
namespace PodLoad.Common.Services
42
{
53
public interface IHostInfoService
64
{

src/PodLoad.Server/Components/Layout/MainLayout.razor

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<MudSnackbarProvider/>
77
<MudLayout>
88
<MudAppBar Elevation="1">
9-
<MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@((e) => DrawerToggle())"/>
9+
<MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@(_ => DrawerToggle())"/>
1010
<MudText Typo="Typo.h5" Class="ml-3">PodLoad Server</MudText>
1111
<MudSpacer/>
1212
<MudIconButton Icon="@(DarkLightModeButtonIcon)" Color="Color.Inherit" OnClick="@DarkModeToggle" Edge="Edge.End"/>
@@ -35,7 +35,7 @@
3535
{
3636
base.OnInitialized();
3737

38-
_theme = new()
38+
_theme = new MudTheme
3939
{
4040
PaletteLight = _lightPalette,
4141
PaletteDark = _darkPalette,
@@ -60,7 +60,7 @@
6060
AppbarBackground = "rgba(255,255,255,0.8)",
6161
DrawerBackground = "#ffffff",
6262
GrayLight = "#e8e8e8",
63-
GrayLighter = "#f9f9f9",
63+
GrayLighter = "#f9f9f9"
6464
};
6565

6666
private readonly PaletteDark _darkPalette = new()
@@ -89,13 +89,13 @@
8989
LinesDefault = "#33323e",
9090
TableLines = "#33323e",
9191
Divider = "#292838",
92-
OverlayLight = "#1e1e2d80",
92+
OverlayLight = "#1e1e2d80"
9393
};
9494

9595
public string DarkLightModeButtonIcon => _isDarkMode switch
9696
{
9797
true => Icons.Material.Rounded.AutoMode,
98-
false => Icons.Material.Outlined.DarkMode,
98+
false => Icons.Material.Outlined.DarkMode
9999
};
100100

101101
}

src/PodLoad.Server/Components/Pages/Clients.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<MudText>No clients connected</MudText>
7373
</NoRecordsContent>
7474
<PagerContent>
75-
<MudTablePager PageSizeOptions="new int[] { 10, 25, 50, 100 }" />
75+
<MudTablePager PageSizeOptions="new [] { 10, 25, 50, 100 }" />
7676
</PagerContent>
7777
</MudTable>
7878

@@ -131,9 +131,9 @@
131131
}
132132
}
133133

134-
private IEnumerable<string> ValidatePercentage(int input)
134+
private static IEnumerable<string> ValidatePercentage(int input)
135135
{
136-
if (input < 0 || input > 100)
136+
if (input is < 0 or > 100)
137137
yield return "Value must be between 0 and 100";
138138
}
139139

src/PodLoad.Server/Models/ClientInfo.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Net;
2-
3-
namespace PodLoad.Server.Models;
1+
namespace PodLoad.Server.Models;
42

53
public class ClientInfo
64
{

src/PodLoad.Server/Services/ClientsInventoryService.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ClientReportResponse ProcessClientReport(ClientReportRequest request)
2121
EnsurePeriodicUpdateEnabled();
2222
using var scope = _clientsLock.EnterScope();
2323

24-
logger.LogTrace("Received client report with ID: {0}", request.ClientId);
24+
logger.LogTrace("Received client report with ID: {clientId}", request.ClientId);
2525

2626
if (_clients.TryGetValue(request.ClientId, out var clientInfo))
2727
{
@@ -31,13 +31,13 @@ public ClientReportResponse ProcessClientReport(ClientReportRequest request)
3131
clientInfo.MemoryAllocated = request.MemoryAllocated;
3232
clientInfo.State = ClientState.Active;
3333

34-
logger.LogTrace("Updating client with ID: {0}", clientInfo.Id);
34+
logger.LogTrace("Updating client with ID: {clientId}", clientInfo.Id);
3535
ClientUpdated?.Invoke(this, clientInfo);
3636
}
3737
else
3838
{
3939
// Create new client, fire event and return response from the created client
40-
clientInfo = new ClientInfo()
40+
clientInfo = new ClientInfo
4141
{
4242
Id = request.ClientId,
4343
HostName = request.ClientHostName,
@@ -50,11 +50,11 @@ public ClientReportResponse ProcessClientReport(ClientReportRequest request)
5050
};
5151
_clients[clientInfo.Id] = clientInfo;
5252

53-
logger.LogInformation("Added new client with ID: {0}", clientInfo.Id);
53+
logger.LogInformation("Added new client with ID: {clientId}", clientInfo.Id);
5454
ClientAdded?.Invoke(this, clientInfo);
5555
}
5656

57-
return new ClientReportResponse()
57+
return new ClientReportResponse
5858
{
5959
DesiredPercentage = clientInfo.DesiredPercentage,
6060
DesiredMemoryAllocated = clientInfo.DesiredMemoryAllocated,
@@ -94,7 +94,7 @@ private async Task CheckForClientsTimeout()
9494
.Where(client => client.State == ClientState.Timeout || DateTime.Now - client.LastUpdated > _keepAliveInterval)
9595
.ToArray();
9696

97-
if (timeOutedClients.Any())
97+
if (timeOutedClients.Length != 0)
9898
{
9999
using var scope = _clientsLock.EnterScope();
100100

@@ -103,13 +103,13 @@ private async Task CheckForClientsTimeout()
103103
if (client.State == ClientState.Timeout)
104104
{
105105
_clients.Remove(client.Id);
106-
logger.LogInformation("Removed client with ID: {0}", client.Id);
106+
logger.LogInformation("Removed client with ID: {clientId}", client.Id);
107107
ClientRemoved?.Invoke(this, client);
108108
}
109109
else
110110
{
111111
client.State = ClientState.Timeout;
112-
logger.LogInformation("First timeout for client with ID: {0}", client.Id);
112+
logger.LogInformation("First timeout for client with ID: {clientId}", client.Id);
113113
ClientUpdated?.Invoke(this, client);
114114
}
115115
}

0 commit comments

Comments
 (0)