Skip to content

Commit c81801a

Browse files
committed
gh-181 Use NLog
- Update docker compose to bring up ELK - Use fo-dicom.NLog
1 parent b2f5018 commit c81801a

File tree

8 files changed

+99
-73
lines changed

8 files changed

+99
-73
lines changed

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ RUN dotnet publish -c Release -o out --nologo src/InformaticsGateway/Monai.Deplo
2828
# Build runtime image
2929
FROM mcr.microsoft.com/dotnet/aspnet:6.0-jammy
3030

31+
# Enable elastic client compatibility mode
32+
ENV ELASTIC_CLIENT_APIVERSIONING=true
3133
ENV DEBIAN_FRONTEND=noninteractive
34+
3235
RUN apt-get clean \
3336
&& apt-get update \
3437
&& apt-get install -y --no-install-recommends \

docker-compose/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ services:
8787
ELASTIC_PASSWORD: ${ELASTIC_PASSWORD:-}
8888
ES_JAVA_OPTS: "-Xmx256m -Xms256m"
8989
discovery.type: single-node
90+
ELASTIC_CLIENT_APIVERSIONING: "true"
9091
networks:
9192
- monaideploy
9293

src/InformaticsGateway/Monai.Deploy.InformaticsGateway.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--
1+
<!--
22
~ Copyright 2022 MONAI Consortium
33
~
44
~ Licensed under the Apache License, Version 2.0 (the "License");
@@ -34,8 +34,9 @@
3434
<ItemGroup>
3535
<PackageReference Include="Ardalis.GuardClauses" Version="4.0.1" />
3636
<PackageReference Include="DotNext.Threading" Version="4.7.2" />
37-
<PackageReference Include="Elasticsearch.Extensions.Logging" Version="1.6.0-alpha1" />
37+
<PackageReference Include="Elastic.CommonSchema.NLog" Version="1.6.0-alpha1" />
3838
<PackageReference Include="fo-dicom" Version="5.0.3" />
39+
<PackageReference Include="fo-dicom.NLog" Version="5.0.3" />
3940
<PackageReference Include="Karambolo.Extensions.Logging.File" Version="3.3.1" />
4041
<PackageReference Include="HL7-dotnetcore" Version="2.29.0" />
4142
<PackageReference Include="GitVersion.MsBuild" Version="5.10.3">
@@ -51,6 +52,9 @@
5152
<PackageReference Include="Monai.Deploy.Messaging.RabbitMQ" Version="0.1.6" />
5253
<PackageReference Include="Monai.Deploy.Storage" Version="0.2.7" />
5354
<PackageReference Include="Monai.Deploy.Storage.MinIO" Version="0.2.7" />
55+
<PackageReference Include="NLog.Targets.ElasticSearch" Version="7.7.0" />
56+
<PackageReference Include="NLog.Web.AspNetCore" Version="5.1.4" />
57+
<PackageReference Include="NLog" Version="5.0.4" />
5458
<PackageReference Include="Polly" Version="7.2.3" />
5559
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
5660
</ItemGroup>
@@ -82,6 +86,7 @@
8286
<ItemGroup>
8387
<None Include="./appsettings.json" CopyToOutputDirectory="Always" />
8488
<None Include="./appsettings.Development.json" CopyToOutputDirectory="Always" />
89+
<None Include="./nlog.config" CopyToOutputDirectory="Always" />
8590
</ItemGroup>
8691

8792
<Target Name="CopyPlugins" AfterTargets="Publish">
@@ -99,4 +104,6 @@
99104
<Copy SourceFiles="@(PluginDlls)" DestinationFolder="$(OutDir)\plug-ins\" SkipUnchangedFiles="true" />
100105
<Message Text="Files copied successfully to $(OutDir)\plug-ins\." Importance="high" />
101106
</Target>
107+
108+
<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JsonSchema="https://json.schemastore.org/band-manifest.json" /></VisualStudio></ProjectExtensions>
102109
</Project>

src/InformaticsGateway/Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
using System.IO;
1919
using System.IO.Abstractions;
2020
using Ardalis.GuardClauses;
21-
using Elasticsearch.Extensions.Logging;
21+
using FellowOakDicom.Log;
2222
using Microsoft.AspNetCore.Hosting;
2323
using Microsoft.EntityFrameworkCore;
2424
using Microsoft.Extensions.Configuration;
@@ -45,6 +45,7 @@
4545
using Monai.Deploy.Messaging.Configuration;
4646
using Monai.Deploy.Storage;
4747
using Monai.Deploy.Storage.Configuration;
48+
using NLog.Web;
4849

4950
namespace Monai.Deploy.InformaticsGateway
5051
{
@@ -84,11 +85,10 @@ internal static IHostBuilder CreateHostBuilder(string[] args) =>
8485
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false)
8586
.AddEnvironmentVariables();
8687
})
87-
.ConfigureLogging((builderContext, configureLogging) =>
88+
.ConfigureLogging((builderContext, builder) =>
8889
{
89-
configureLogging.AddConfiguration(builderContext.Configuration.GetSection("Logging"));
90-
configureLogging.AddFile(o => o.RootPath = AppContext.BaseDirectory);
91-
configureLogging.AddElasticsearch();
90+
builder.ClearProviders();
91+
builder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
9292
})
9393
.ConfigureServices((hostContext, services) =>
9494
{
@@ -120,14 +120,13 @@ internal static IHostBuilder CreateHostBuilder(string[] args) =>
120120

121121
services.AddMonaiDeployStorageService(hostContext.Configuration.GetSection("InformaticsGateway:storage:serviceAssemblyName").Value, Monai.Deploy.Storage.HealthCheckOptions.ServiceHealthCheck);
122122

123-
124123
services.AddMonaiDeployMessageBrokerPublisherService(hostContext.Configuration.GetSection("InformaticsGateway:messaging:publisherServiceAssemblyName").Value, true);
125124
services.AddMonaiDeployMessageBrokerSubscriberService(hostContext.Configuration.GetSection("InformaticsGateway:messaging:subscriberServiceAssemblyName").Value, true);
126125

127126
services.AddSingleton<ConfigurationValidator>();
128127
services.AddSingleton<IObjectUploadQueue, ObjectUploadQueue>();
129128
services.AddSingleton<IPayloadAssembler, PayloadAssembler>();
130-
services.AddSingleton<FellowOakDicom.Log.ILogManager, Logging.FoDicomLogManager>();
129+
services.AddSingleton<FellowOakDicom.Log.ILogManager, NLogManager>();
131130
services.AddSingleton<IMonaiServiceLocator, MonaiServiceLocator>();
132131
services.AddSingleton<IStorageInfoProvider, StorageInfoProvider>();
133132
services.AddSingleton<IMonaiAeChangedNotificationService, MonaiAeChangedNotificationService>();
@@ -174,6 +173,7 @@ internal static IHostBuilder CreateHostBuilder(string[] args) =>
174173
webBuilder.ConfigureKestrel(options => options.Limits.MaxRequestBodySize = int.MaxValue);
175174
webBuilder.CaptureStartupErrors(true);
176175
webBuilder.UseStartup<Startup>();
177-
});
176+
})
177+
.UseNLog();
178178
}
179179
}

src/InformaticsGateway/Properties/launchSettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"Monai.Deploy.InformaticsGateway": {
44
"commandName": "Project",
55
"environmentVariables": {
6-
"DOTNET_ENVIRONMENT": "Development"
6+
"DOTNET_ENVIRONMENT": "Development",
7+
"ELASTIC_CLIENT_APIVERSIONING": "true"
78
}
89
}
910
}

src/InformaticsGateway/appsettings.Development.json

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,5 @@
3535
"securedConnection": false
3636
}
3737
}
38-
},
39-
"Logging": {
40-
"LogLevel": {
41-
"Dicom": "Information",
42-
"Monai": "Debug",
43-
"Microsoft": "Information",
44-
"Microsoft.EntityFrameworkCore": "Warning",
45-
"Microsoft.Hosting.Lifetime": "Information",
46-
"Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker": "Information"
47-
},
48-
"Console": {
49-
"FormatterName": "Simple",
50-
"FormatterOptions": {
51-
"ColorBehavior": "Enabled",
52-
"IncludeScopes": true,
53-
"SingleLine": false,
54-
"TimestampFormat": " HH:mm:ss ",
55-
"UseUtcTimestamp": false
56-
}
57-
}
5838
}
5939
}

src/InformaticsGateway/appsettings.json

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -63,48 +63,6 @@
6363
"sendAck": true
6464
}
6565
},
66-
"Logging": {
67-
"LogLevel": {
68-
"Default": "Information",
69-
"Dicom": "Information",
70-
"System": "Warning",
71-
"Microsoft": "Warning",
72-
"Microsoft.EntityFrameworkCore": "Warning",
73-
"Microsoft.Hosting.Lifetime": "Warning",
74-
"Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker": "Error",
75-
"Monai": "Information"
76-
},
77-
"Console": {
78-
"FormatterName": "Systemd",
79-
"FormatterOptions": {
80-
"ColorBehavior": "Disabled",
81-
"IncludeScopes": true,
82-
"SingleLine": false,
83-
"TimestampFormat": " HH:mm:ss ",
84-
"UseUtcTimestamp": true
85-
}
86-
},
87-
"Elasticsearch": {
88-
"Index": "monai-deploy-{0:yyyy.MM.dd}",
89-
"NodeUris": [ "https://localhost:9200" ],
90-
"Tags": [ "Development" ]
91-
},
92-
"File": {
93-
"BasePath": "logs",
94-
"FileEncodingName": "utf-8",
95-
"DateFormat": "yyyyMMdd",
96-
"CounterFormat": "000",
97-
"MaxFileSize": 10485760,
98-
"IncludeScopes": true,
99-
"MaxQueueSize": 100,
100-
"TextBuilderType": "Monai.Deploy.InformaticsGateway.Logging.FileLoggingTextFormatter, Monai.Deploy.InformaticsGateway",
101-
"Files": [
102-
{
103-
"Path": "MIG-<date>-<counter>.log"
104-
}
105-
]
106-
}
107-
},
10866
"Kestrel": {
10967
"EndPoints": {
11068
"Http": {

src/InformaticsGateway/nlog.config

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
autoReload="true"
5+
internalLogLevel="Debug"
6+
internalLogFile="${basedir}/logs/internal-nlog.txt">
7+
8+
<extensions>
9+
<add assembly="NLog.Web.AspNetCore"/>
10+
<add assembly="NLog.Extensions.Logging"/>
11+
<add assembly="NLog.Targets.ElasticSearch"/>
12+
<add assembly="Elastic.CommonSchema.NLog"/>
13+
</extensions>
14+
15+
<variable name="logDir" value="${basedir}/logs/" />
16+
<!-- used for Elastic -->
17+
<variable name="hostUri" value="http://localhost:9200" />
18+
<variable name="indexName" value="monai-deploy-informatics-gateway-${date:format=yyyy.MM.dd}" />
19+
20+
<time type="AccurateUTC" />
21+
22+
<targets async="true">
23+
<!-- File Target for all log messages with basic details -->
24+
<target xsi:type="File" name="allfile" fileName="${logDir}/MIG-${shortdate}.log" >
25+
<layout xsi:type="JsonLayout" includeEventProperties="true" includeScopeProperties="true">
26+
<attribute name="timestamp" layout="${date:format=o}" />
27+
<attribute name="level" layout="${level:upperCase=true}"/>
28+
<attribute name="logger" layout="${logger}" />
29+
<attribute name="message" layout="${message:raw=true}" />
30+
<attribute name="properties" layout="${message:raw=true}" />
31+
</layout>
32+
</target>
33+
34+
35+
<!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection -->
36+
<target xsi:type="ColoredConsole" name="lifetimeConsole" layout="${longdate}|${event-properties:item=EventId:whenEmpty=0}|${uppercase:${level}}|${logger}|${scopenested}|${message} ${exception:format=tostring}" />
37+
38+
<target type="ElasticSearch, NLog.Targets.ElasticSearch"
39+
name="elastic"
40+
uri="${hostUri}"
41+
index="${indexName}"
42+
pipeline=""
43+
documentType=""
44+
includeAllProperties="true"
45+
excludedProperties=""
46+
opCodeCreate="true"
47+
maxRecursionLimit="1"
48+
disableAutomaticProxyDetection="true"
49+
disablePing="false"
50+
EnableHttpCompression="false"
51+
DisableCertificateValidation="false"
52+
enableJsonLayout="true"
53+
>
54+
<layout type="EcsLayout">
55+
<metadata name="IncludeEventProperties" layout="true" />
56+
<metadata name="IncludeScopeProperties" layout="true" />
57+
</layout>
58+
</target>
59+
</targets>
60+
61+
<!-- rules to map from logger name to target -->
62+
<rules>
63+
<!--All logs, including from Microsoft-->
64+
<logger name="*" minlevel="Trace" writeTo="allfile, elastic" />
65+
66+
<!--Output hosting lifetime messages to console target for faster startup detection -->
67+
<logger name="Microsoft.EntityFrameworkCore" minlevel="Warn" writeTo="lifetimeConsole" final="true" />
68+
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole" final="true" />
69+
<logger name="Monai.*" minlevel="Trace" writeTo="lifetimeConsole" final="true" />
70+
71+
<!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) -->
72+
<logger name="Microsoft.*" maxlevel="Info" final="true" />
73+
<logger name="System.Net.Http.*" maxlevel="Info" final="true" />
74+
75+
</rules>
76+
</nlog>

0 commit comments

Comments
 (0)