Shared Service Settings
You may have noticed that some settings in the appsettings.json
files will have the same values across the different services.
It is possible to share those settings in a shared settings file by referring to it with the json setting SharedSettingFiles
.
Note
The description of the Serilog
settings only apply to framework versions before v5. Starting with framework v5 they can be removed
as all logging settings are migrated and consolidated in a single new BamConfig
section of a general appsettings.json file. It means
that these separate Serilog
settings are ignored if still present.
For more information, read the article BAM Configuration Settings Migration.
Old Situation
Let's say you have the following setting files for the Authority Service
and the Core Service
.
Old Core Service Settings File
{
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\Program Files (x86)\\Ometa BVBA\\Log\\Services\\Ometa.Framework.Web.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fffffff} [{Level}-{ThreadId}]: {Message}{NewLine}{Exception}",
"shared": false,
"retainedFileCountLimit": 24,
"fileSizeLimitBytes": 2097152,
"rollOnFileSizeLimit": true,
"rollingInterval": "Day"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithProcessId", "WithProcessName" ],
"Properties": {
"Application": "Ometa.Framework.Web"
}
},
"ConnectionStrings": {
"OmetaFrameworkDatabase": "Data Source=sqlserver.yourdomain.net;database=yourframeworkdatabase;trusted_connection=yes;"
}
}
Old Authority Service Settings File
{
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\Program Files (x86)\\Ometa BVBA\\Log\\Services\\Ometa.Framework.Authority.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fffffff} [{Level}-{ThreadId}]: {Message}{NewLine}{Exception}",
"shared": false,
"retainedFileCountLimit": 24,
"fileSizeLimitBytes": 2097152,
"rollOnFileSizeLimit": true,
"rollingInterval": "Day"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithProcessId", "WithProcessName" ],
"Properties": {
"Application": "Ometa.Framework.Authority"
}
},
"SigningCertificate": "CN=*.yourdomain.net",
"ConnectionStrings": {
"OmetaFrameworkDatabase": "Data Source=sqlserver.yourdomain.net;database=yourframeworkdatabase;trusted_connection=yes;"
}
}
You notice that the section of Serilog
and the ConnectionStrings
are roughly having the same values. The Serilog section uses another log filename but this can be fixed by using a property in the configuration.
New Situation
Create the Shared Settings File
Create a new json settings file on a location of your choice with a name of your choice. We will be using appsettings.shared.json
as the name and put it in the C:\Program Files (x86)\Ometa BVBA\Config
directory.
Move the Shared Settings
Move the shared settings you'll want to use for multiple services to this new file. In our example the appsettings.shared.json
will contain the following settings.
{
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\Program Files (x86)\\Ometa BVBA\\Log\\Services\\Ometa.Framework.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fffffff} [{Application}] [{Level}] [{ThreadId}]: {Message}{NewLine}{Exception}",
"shared": true,
"retainedFileCountLimit": 24,
"fileSizeLimitBytes": 2097152,
"rollOnFileSizeLimit": true,
"rollingInterval": "Day"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithProcessId", "WithProcessName" ]
},
"ConnectionStrings": {
"OmetaFrameworkDatabase": "Data Source=sqlserver.yourdomain.net;database=yourframeworkdatabase;trusted_connection=yes;"
}
}
Important
If you want to share the Serilog
settings, make sure to set the shared
setting to true
because more than one application will be logging to the file.
Also make sure to use the name of the application in the outputTemplate
so you can identify from which application a specific log entry was written.
This is taken into account in the example above.
Referring the Shared Settings
Now you'll have to refer to this shared settings file in the appsettings.json
of the services.
New Core Service Settings File
{
"SharedSettingFiles": [
"C:\\Program Files (x86)\\Ometa BVBA\\Config\\appsettings.shared.json"
]
}
New Authority Service Settings File
{
"SharedSettingFiles": [
"C:\\Program Files (x86)\\Ometa BVBA\\Config\\appsettings.shared.json"
],
"SigningCertificate": "CN=*.yourdomain.net"
}
Working With Shared Settings
Tip
You can refer to other shared setting files within a shared settings file. The system itself will detect if a shared settings file is already loaded or not.
You can refer to more than one shared settings file in your appsettings.json
. All settings will be merged together.
However, if you do this and the same settings entry (e.g. "ConnectionStrings/OmetaFrameworkDatabase") is found more than once, the last found value will be used.
Note
If you refer to a non-existing json file, it will be skipped.
Important
When working with directory paths in a json file, remember to escape the '\' character by using '\\'.
If one of the referred setting files contain invalid json content, the service(s) will not be able to start.