Skip to content

Commit b3b3049

Browse files
committed
gh-65 address warnings & messages reported by sonarqube
- Use structured logging in .net 6 Signed-off-by: Victor Chang <[email protected]>
1 parent 8b7fc7f commit b3b3049

File tree

97 files changed

+1069
-472
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+1069
-472
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ jobs:
8787

8888
- name: Perform CodeQL Analysis
8989
uses: github/codeql-action/analyze@v1
90-
9190

9291
unit-test:
9392
runs-on: windows-latest
@@ -139,16 +138,37 @@ jobs:
139138
run: dotnet restore
140139
working-directory: ./src
141140

142-
- name: Build and analyze
141+
- name: Begin SonarScanner
143142
env:
144143
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
145144
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
146145
shell: powershell
147-
run: |
148-
.\.sonar\scanner\dotnet-sonarscanner begin /k:"Project-MONAI_monai-deploy-informatics-gateway" /o:"project-monai" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="src\${{ env.TEST_RESULTS }}\**\*.xml"
149-
dotnet build -c ${{ env.BUILD_CONFIG }} --nologo "src\${{ env.SOLUTION }}"
150-
dotnet test --no-build --filter FullyQualifiedName\!~Monai.Deploy.InformaticsGateway.Integration.Test -c ${{ env.BUILD_CONFIG }} -v=minimal --results-directory "src\${{ env.TEST_RESULTS }}" --collect:"XPlat Code Coverage" --settings src\coverlet.runsettings "src\${{ env.SOLUTION }}"
151-
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
146+
run: .\.sonar\scanner\dotnet-sonarscanner begin /k:"Project-MONAI_monai-deploy-informatics-gateway" /o:"project-monai" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="src\${{ env.TEST_RESULTS }}\**\*.xml"
147+
148+
- name: Build
149+
env:
150+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
151+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
152+
shell: powershell
153+
run: dotnet build -c ${{ env.BUILD_CONFIG }} --nologo "${{ env.SOLUTION }}"
154+
working-directory: ./src
155+
156+
- name: Test
157+
env:
158+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
159+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
160+
shell: powershell
161+
run: dotnet test --no-build --filter FullyQualifiedName\!~Monai.Deploy.InformaticsGateway.Integration.Test -c ${{ env.BUILD_CONFIG }} -v=minimal --results-directory "${{ env.TEST_RESULTS }}" --collect:"XPlat Code Coverage" --settings coverlet.runsettings "${{ env.SOLUTION }}"
162+
working-directory: ./src
163+
164+
165+
- name: End SonarScanner
166+
env:
167+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
168+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
169+
shell: powershell
170+
run: .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
171+
152172

153173
- uses: codecov/codecov-action@v2
154174
with:
@@ -298,7 +318,6 @@ jobs:
298318
tests/Integration.Test/run.log
299319
retention-days: 30
300320

301-
302321
docs:
303322
runs-on: ubuntu-latest
304323
needs: [calc-version]

src/Api/Monai.Deploy.InformaticsGateway.Api.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ SPDX-License-Identifier: Apache License 2.0
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="GitVersion.MsBuild" Version="5.8.2">
16+
<PackageReference Include="GitVersion.MsBuild" Version="5.9.0">
1717
<PrivateAssets>All</PrivateAssets>
1818
</PackageReference>
1919
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />

src/Api/Storage/FileStorageInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void SetWorkflows(params string[] workflows)
178178
/// </summary>
179179
protected virtual string GenerateStoragePath()
180180
{
181-
string filePath = System.IO.Path.Combine(StorageRootPath, $"{CorrelationId}-{MessageId}") + FileExtension;
181+
var filePath = System.IO.Path.Combine(StorageRootPath, $"{CorrelationId}-{MessageId}") + FileExtension;
182182
filePath = filePath.ToLowerInvariant();
183183
var index = 1;
184184
while (FileSystem.File.Exists(filePath))

src/CLI/Commands/AetCommand.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Ardalis.GuardClauses;
1414
using Microsoft.Extensions.DependencyInjection;
1515
using Microsoft.Extensions.Hosting;
16-
using Microsoft.Extensions.Logging;
1716
using Monai.Deploy.InformaticsGateway.Api;
1817
using Monai.Deploy.InformaticsGateway.CLI.Services;
1918
using Monai.Deploy.InformaticsGateway.Client;
@@ -109,22 +108,22 @@ private async Task<int> ListAeTitlehandlerAsync(IHost host, bool verbose, Cancel
109108
client.ConfigureServiceUris(configService.Configurations.InformaticsGatewayServerUri);
110109
LogVerbose(verbose, host, $"Connecting to {Strings.ApplicationName} at {configService.Configurations.InformaticsGatewayServerEndpoint}...");
111110
LogVerbose(verbose, host, $"Retrieving MONAI SCP AE Titles...");
112-
items = await client.MonaiScpAeTitle.List(cancellationToken);
111+
items = await client.MonaiScpAeTitle.List(cancellationToken).ConfigureAwait(false);
113112
}
114113
catch (ConfigurationException ex)
115114
{
116-
logger.Log(LogLevel.Critical, ex.Message);
115+
logger.ConfigurationException(ex.Message);
117116
return ExitCodes.Config_NotConfigured;
118117
}
119118
catch (Exception ex)
120119
{
121-
logger.Log(LogLevel.Critical, $"Error retrieving MONAI SCP AE Titles: {ex.Message}");
120+
logger.ErrorListingMonaiAeTitles(ex.Message);
122121
return ExitCodes.MonaiScp_ErrorList;
123122
}
124123

125124
if (items.IsNullOrEmpty())
126125
{
127-
logger.Log(LogLevel.Warning, "No MONAI SCP Application Entities configured.");
126+
logger.NoAeTitlesFound();
128127
}
129128
else
130129
{
@@ -166,17 +165,17 @@ private async Task<int> RemoveAeTitlehandlerAsync(string name, IHost host, bool
166165
client.ConfigureServiceUris(configService.Configurations.InformaticsGatewayServerUri);
167166
LogVerbose(verbose, host, $"Connecting to {Strings.ApplicationName} at {configService.Configurations.InformaticsGatewayServerEndpoint}...");
168167
LogVerbose(verbose, host, $"Deleting MONAI SCP AE Title {name}...");
169-
_ = await client.MonaiScpAeTitle.Delete(name, cancellationToken);
170-
logger.Log(LogLevel.Information, $"MONAI SCP AE Title '{name}' deleted.");
168+
_ = await client.MonaiScpAeTitle.Delete(name, cancellationToken).ConfigureAwait(false);
169+
logger.MonaiAeTitleDeleted(name);
171170
}
172171
catch (ConfigurationException ex)
173172
{
174-
logger.Log(LogLevel.Critical, ex.Message);
173+
logger.ConfigurationException(ex.Message);
175174
return ExitCodes.Config_NotConfigured;
176175
}
177176
catch (Exception ex)
178177
{
179-
logger.Log(LogLevel.Critical, $"Error deleting MONAI SCP AE Title {name}: {ex.Message}");
178+
logger.ErrorDeletingMonaiAeTitle(name, ex.Message);
180179
return ExitCodes.MonaiScp_ErrorDelete;
181180
}
182181
return ExitCodes.Success;
@@ -202,33 +201,29 @@ private async Task<int> AddAeTitlehandlerAsync(MonaiApplicationEntity entity, IH
202201
client.ConfigureServiceUris(configService.Configurations.InformaticsGatewayServerUri);
203202

204203
LogVerbose(verbose, host, $"Connecting to {Strings.ApplicationName} at {configService.Configurations.InformaticsGatewayServerEndpoint}...");
205-
var result = await client.MonaiScpAeTitle.Create(entity, cancellationToken);
204+
var result = await client.MonaiScpAeTitle.Create(entity, cancellationToken).ConfigureAwait(false);
206205

207-
logger.Log(LogLevel.Information, "New MONAI Deploy SCP Application Entity created:");
208-
logger.Log(LogLevel.Information, $"\tName: {result.Name}");
209-
logger.Log(LogLevel.Information, $"\tAE Title: {result.AeTitle}");
210-
logger.Log(LogLevel.Information, $"\tGrouping: {result.Grouping}");
211-
logger.Log(LogLevel.Information, $"\tTimeout: {result.Grouping}s");
206+
logger.MonaiAeTitleCreated(result.Name, result.AeTitle, result.Grouping, result.Timeout);
212207

213208
if (result.Workflows.Any())
214209
{
215-
logger.Log(LogLevel.Information, $"\tWorkflows:{string.Join(',', result.Workflows)}");
216-
logger.Log(LogLevel.Warning, "Data received by this Application Entity will bypass Data Routing Service.");
210+
logger.MonaiAeWorkflows(string.Join(',', result.Workflows));
211+
logger.WorkflowWarning();
217212
}
218213
if (result.IgnoredSopClasses.Any())
219214
{
220-
logger.Log(LogLevel.Information, $"\tIgnored SOP Classes:{string.Join(',', result.IgnoredSopClasses)}");
221-
logger.Log(LogLevel.Warning, "Instances with matching SOP class UIDs are accepted but dropped.");
215+
logger.MonaiAeIgnoredSops(string.Join(',', result.IgnoredSopClasses));
216+
logger.IgnoreSopClassesWarning();
222217
}
223218
}
224219
catch (ConfigurationException ex)
225220
{
226-
logger.Log(LogLevel.Critical, ex.Message);
221+
logger.ConfigurationException(ex.Message);
227222
return ExitCodes.Config_NotConfigured;
228223
}
229224
catch (Exception ex)
230225
{
231-
logger.Log(LogLevel.Critical, $"Error creating MONAI SCP AE Title {entity.AeTitle}: {ex.Message}");
226+
logger.MonaiAeCreateCritical(entity.AeTitle, ex.Message);
232227
return ExitCodes.MonaiScp_ErrorCreate;
233228
}
234229
return ExitCodes.Success;

src/CLI/Commands/CommandBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected static void LogVerbose(bool verbose, IHost host, string message)
4040
}
4141
else
4242
{
43-
logger.Log(LogLevel.Debug, message);
43+
logger.DebugMessage(message);
4444
}
4545
}
4646
}

src/CLI/Commands/ConfigCommand.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,19 @@ private int ShowConfigurationHandler(IHost host, bool verbose, CancellationToken
8282
try
8383
{
8484
CheckConfiguration(configService);
85-
logger.Log(LogLevel.Information, $"Informatics Gateway API: {configService.Configurations.InformaticsGatewayServerEndpoint}");
86-
logger.Log(LogLevel.Information, $"DICOM SCP Listening Port: {configService.Configurations.DicomListeningPort}");
87-
logger.Log(LogLevel.Information, $"Container Runner: {configService.Configurations.Runner}");
88-
logger.Log(LogLevel.Information, $"Host:");
89-
logger.Log(LogLevel.Information, $" Database storage mount: {configService.Configurations.HostDatabaseStorageMount}");
90-
logger.Log(LogLevel.Information, $" Data storage mount: {configService.Configurations.HostDataStorageMount}");
91-
logger.Log(LogLevel.Information, $" Logs storage mount: {configService.Configurations.HostLogsStorageMount}");
85+
logger.ConfigInformaticsGatewayApiEndpoint(configService.Configurations.InformaticsGatewayServerEndpoint);
86+
logger.ConfigDicomScpPort(configService.Configurations.DicomListeningPort);
87+
logger.ConfigContainerRunner(configService.Configurations.Runner);
88+
logger.ConfigHostInfo(configService.Configurations.HostDatabaseStorageMount, configService.Configurations.HostDataStorageMount, configService.Configurations.HostLogsStorageMount);
9289
}
93-
catch (ConfigurationException)
90+
catch (ConfigurationException ex)
9491
{
92+
logger.ConfigurationException(ex.Message);
9593
return ExitCodes.Config_NotConfigured;
9694
}
9795
catch (Exception ex)
9896
{
99-
logger.Log(LogLevel.Error, ex.Message);
97+
logger.CriticalException(ex.Message);
10098
return ExitCodes.Config_ErrorShowing;
10199
}
102100
return ExitCodes.Success;
@@ -118,13 +116,14 @@ private static int ConfigUpdateHandler(IHost host, Action<IConfigurationService>
118116
updater(config);
119117
logger.Log(LogLevel.Information, "Configuration updated successfully.");
120118
}
121-
catch (ConfigurationException)
119+
catch (ConfigurationException ex)
122120
{
121+
logger.ConfigurationException(ex.Message);
123122
return ExitCodes.Config_NotConfigured;
124123
}
125124
catch (Exception ex)
126125
{
127-
logger.Log(LogLevel.Error, ex.Message);
126+
logger.CriticalException(ex.Message);
128127
return ExitCodes.Config_ErrorSaving;
129128
}
130129
return ExitCodes.Success;
@@ -152,7 +151,7 @@ private async Task<int> InitHandlerAsync(IHost host, bool verbose, bool yes, Can
152151
}
153152
catch (Exception ex)
154153
{
155-
logger.Log(LogLevel.Error, ex.Message);
154+
logger.CriticalException(ex.Message);
156155
return ExitCodes.Config_ErrorInitializing;
157156
}
158157
return ExitCodes.Success;

src/CLI/Commands/ConfigurationException.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ namespace Monai.Deploy.InformaticsGateway.CLI
99
[Serializable]
1010
public class ConfigurationException : Exception
1111
{
12+
private ConfigurationException()
13+
{
14+
}
15+
1216
public ConfigurationException(string message) : base(message)
1317
{
1418
}
1519

20+
public ConfigurationException(string message, Exception innerException) : base(message, innerException)
21+
{
22+
}
23+
1624
protected ConfigurationException(SerializationInfo info, StreamingContext context) : base(info, context)
1725
{
1826
}

src/CLI/Commands/DestinationCommand.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Ardalis.GuardClauses;
1414
using Microsoft.Extensions.DependencyInjection;
1515
using Microsoft.Extensions.Hosting;
16-
using Microsoft.Extensions.Logging;
1716
using Monai.Deploy.InformaticsGateway.Api;
1817
using Monai.Deploy.InformaticsGateway.CLI.Services;
1918
using Monai.Deploy.InformaticsGateway.Client;
@@ -97,22 +96,22 @@ private async Task<int> ListDestinationHandlerAsync(DestinationApplicationEntity
9796
client.ConfigureServiceUris(configService.Configurations.InformaticsGatewayServerUri);
9897
LogVerbose(verbose, host, $"Connecting to {Strings.ApplicationName} at {configService.Configurations.InformaticsGatewayServerEndpoint}...");
9998
LogVerbose(verbose, host, $"Retrieving DICOM destinations...");
100-
items = await client.DicomDestinations.List(cancellationToken);
99+
items = await client.DicomDestinations.List(cancellationToken).ConfigureAwait(false);
101100
}
102101
catch (ConfigurationException ex)
103102
{
104-
logger.Log(LogLevel.Critical, ex.Message);
103+
logger.ConfigurationException(ex.Message);
105104
return ExitCodes.Config_NotConfigured;
106105
}
107106
catch (Exception ex)
108107
{
109-
logger.Log(LogLevel.Critical, $"Error retrieving DICOM destinations: {ex.Message}");
108+
logger.ErrorListingDicomDestinations(ex.Message);
110109
return ExitCodes.DestinationAe_ErrorList;
111110
}
112111

113112
if (items.IsNullOrEmpty())
114113
{
115-
logger.Log(LogLevel.Warning, "No DICOM destinations configured.");
114+
logger.NoDicomDestinationFound();
116115
}
117116
else
118117
{
@@ -155,17 +154,17 @@ private async Task<int> RemoveDestinationHandlerAsync(string name, IHost host, b
155154
client.ConfigureServiceUris(configService.Configurations.InformaticsGatewayServerUri);
156155
LogVerbose(verbose, host, $"Connecting to {Strings.ApplicationName} at {configService.Configurations.InformaticsGatewayServerEndpoint}...");
157156
LogVerbose(verbose, host, $"Deleting DICOM destination {name}...");
158-
_ = await client.DicomDestinations.Delete(name, cancellationToken);
159-
logger.Log(LogLevel.Information, $"DICOM destination '{name}' deleted.");
157+
_ = await client.DicomDestinations.Delete(name, cancellationToken).ConfigureAwait(false);
158+
logger.DicomDestinationDeleted(name);
160159
}
161160
catch (ConfigurationException ex)
162161
{
163-
logger.Log(LogLevel.Critical, ex.Message);
162+
logger.ConfigurationException(ex.Message);
164163
return ExitCodes.Config_NotConfigured;
165164
}
166165
catch (Exception ex)
167166
{
168-
logger.Log(LogLevel.Critical, $"Error deleting DICOM destination {name}: {ex.Message}");
167+
logger.ErrorDeletingDicomDestination(name, ex.Message);
169168
return ExitCodes.DestinationAe_ErrorDelete;
170169
}
171170
return ExitCodes.Success;
@@ -191,22 +190,18 @@ private async Task<int> AddDestinationHandlerAsync(DestinationApplicationEntity
191190
client.ConfigureServiceUris(configService.Configurations.InformaticsGatewayServerUri);
192191

193192
LogVerbose(verbose, host, $"Connecting to {Strings.ApplicationName} at {configService.Configurations.InformaticsGatewayServerEndpoint}...");
194-
var result = await client.DicomDestinations.Create(entity, cancellationToken);
193+
var result = await client.DicomDestinations.Create(entity, cancellationToken).ConfigureAwait(false);
195194

196-
logger.Log(LogLevel.Information, $"New DICOM destination created:");
197-
logger.Log(LogLevel.Information, $"\tName: {result.Name}");
198-
logger.Log(LogLevel.Information, $"\tAE Title: {result.AeTitle}");
199-
logger.Log(LogLevel.Information, $"\tHost/IP Address: {result.HostIp}");
200-
logger.Log(LogLevel.Information, $"\tPort: {result.Port}");
195+
logger.DicomDestinationCreated(result.Name, result.AeTitle, result.HostIp, result.Port);
201196
}
202197
catch (ConfigurationException ex)
203198
{
204-
logger.Log(LogLevel.Critical, ex.Message);
199+
logger.ConfigurationException(ex.Message);
205200
return ExitCodes.Config_NotConfigured;
206201
}
207202
catch (Exception ex)
208203
{
209-
logger.Log(LogLevel.Critical, $"Error creating DICOM destination {entity.AeTitle}: {ex.Message}");
204+
logger.ErrorCreatingDicomDestination(entity.AeTitle, ex.Message);
210205
return ExitCodes.DestinationAe_ErrorCreate;
211206
}
212207
return ExitCodes.Success;

src/CLI/Commands/RestartCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private async Task<int> RestartCommandHandler(IHost host, bool yes, bool verbose
4141

4242
try
4343
{
44-
await service.RestartService(cancellationToken);
44+
await service.RestartService(cancellationToken).ConfigureAwait(false);
4545
}
4646
catch (Exception ex)
4747
{

0 commit comments

Comments
 (0)