asp net core docker serilog configuration - docker

I have a dot net core 3.1 app that is configured to run in docker containers.
While I have managed to update simple appsettings config using docker -e flag, I am still confused when it comes to a bit complex configuration, for example, serilog config.
appsettings file:
{
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Host=172.18.101.65;Port=5432;Username=postgres;Password=pgadmin;Database=identityserver;"
},
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcesssId", "WithThreadId" ],
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "D:\\Logs\\IDSvr\\Log-.txt",
"outputTemplate": "{Timestamp:G} {Message}{NewLine:1}{Exception:1}",
"rollingInterval": "Day",
"shared": true
}
},
{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:8081"
}
}
]
},
.
.
.
}
I could update the connection string using -e ConnectionStrings__DefaultConnection="..." flag, I am not sure how could I update the Serilog's WriteTo configuration section.

WriteTo is a complex object array. Hence, you have to specify the index you want to override. Lets say you want to override:
"Serilog": {
"WriteTo": [
{
"Name": "Console" // this value
},
...
You can select the key by -e Serilog__WriteTo__0__Name due to index 0 in the array. Then just pass it a value you want, like -e Serilog__WriteTo__0__Name=something.
To read that exact value in .NET Core Configuration, use Configuration["Serilog:WriteTo:0:Name"] in the startup pipeline.

Related

Serilog - Not logging to SQL

Hoping someone can provide some information on how to configure Serilog to write to MSSql in an ASP.Net Core DotNet 6 application, I have followed the configurations seen on the Web but nothing seems to work. Serilog logs okay to the console and to file but not to the database.
I have included all my configuration code below:
builder.Host.UseSerilog((ctx, lc) =>
lc.ReadFrom.Configuration(ctx.Configuration));
app.UseSerilogRequestLogging();
Appsetting.jason config:
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.sinks.File", "Serilog.Sinks.MSSqlServer" ],
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "RevIntraConnection",
"tableName": "DBLogs",
"autoCreateSqlTable": false
}
},
{
"Name": "File",
"Args": {
"path": "./Logs/Log-.txt",
"rollingInterval": "Day"
}
},
{ "Name": "Console" }
]
},
Hopefully someone can provide some insight in how to get the correct configuration to make logging to SQL work
Regards
Peter

Disable console sink in release environments?

quick question,
I run console sink for an asp.net core webapp. We use the console sink for development - but for performance i think it would be best to disable the sink in our production environment.
I'm assuming this sink pushes logs to stdout regardless of whether there are any listeners, hence turing the sink off would be better.
Please do correct me if i'm wrong.
Would i be able to turn off the sink under a specified condition/property/commandline arg etc. or would performance not be impacted at all?
Would i be able to disable the sink based on whether there are any listeners?
My config is as follows:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"System": "Warning",
"System.Net.Http.HttpClient": "Warning",
"Hangfire": "Warning"
}
},
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"Enrich": [ "FromLogContext" ],
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {SourceContext}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "%CUROUTDIR%\\..\\Logs\\%PROCESSNAME%\\%PROCESSNAME% .txt",
"rollingInterval": "Day",
"rollOnFileSizeLimit": true,
"retainedFileCountLimit": 31,
"fileSizeLimitBytes": 5242880,
"outPutTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"
}
}
]
}
}

Serilog does not generate logs to console or file when application is published (.net 6)

I have a worker app on .net 6. Serilog works fine (console and file logging) when running application with dotnet run. However, when running from executable app it doesn't log anything (does not even create log file)
I'm compiling the app with:
dotnet publish -r linux-x64 -c Release -o app/publish --self-contained true -p:PublishSingleFile=true -p:PublishReadyToRun=false -p:PublishTrimmed=false
The serilog.json file is:
{
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information",
"System": "Information",
"MyApp.LoadData": "Information"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:o}][{Level:u4}][{ThreadId}][{SourceContext}] {Message}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"buffered": true,
"flushToDiskInterval": 15,
"outputTemplate": "[{Timestamp:o}][{Level:u4}][{ThreadId}][{SourceContext}] {Message}{NewLine}{Exception}",
"path": "customerscredit//logs//LoadCredit.log",
"retainedFileCountLimit": 31,
"rollingInverval": "Day",
"textFormatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId", "WithExceptionDetails"] ,
"Properties": {
"ApplicationName": "MyApp.LoadData",
"Product": "MyApp",
"Env": ""
}
}
}
Am I missing something in the configuration? Is there a specific way of compiling the app for Serilog to work?
Thanks for any help.
Found the issue. From the Serilog.Settings.Configuration documentation:
NET 5.0 Single File Applications Currently, auto-discovery of
configuration assemblies is not supported in bundled mode. DO use
Using section for workaround.
So, all I needed to too was set the Using as follows:
"Using": ["Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Enrichers.Thread"]

Configuring an EC2 (not Fargate) instance via task_definition.json

Currently we have a working task_definition file for a AWS Fargate instance.
We want to migrate from Fargate to a specific AWS EC2 instance, e.g. Z1d.
From the AWS documentation I found that the ecs.instance-type parameter needs to be added.
Unfortunately it does not state where it should be added in the task_definition.json file.
Currently we have something along the lines of:
{
"family": "generic-family",
"requiresCompatibilities": ["FARGATE"],
"cpu": "4096",
"memory": "8192",
...
"containerDefinitions": [
{
"name": "generic-docker-name",
"image": "...",
},
]
}
We think it should be something like:
{
"family": "generic-family",
"requiresCompatibilities": ["EC2"],
"ecs.instance-type": "Z1d",
...
"containerDefinitions": [
{
"name": "generic-docker-name",
"image": "...",
},
]
}
Or looking at some other documentation:
{
"family": "generic-family",
"requiresCompatibilities": ["EC2"],
...
"containerDefinitions": [
{
"name": "generic-docker-name",
"image": "...",
"Parameters": {
"InstanceTypeParameter" : {
"Type" : "String",
"Default" : "z1d.large",
"AllowedValues" : ["z1d.large"],
"Description" : "..."
}
}
},
]
}
But that doesn't seem to work.
Does anyone know how this should be done? Or how I should read the AWS documentation for this specific topic?
Add this to your task_definition.json file at the top level.
"placementConstraints": [
{
"type": "memberOf",
"expression": "attribute:ecs.instance-type == z1d.large"
}
],
You can read more about it here: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html

How to configure Serilog.Exceptions.SqlServer in appsettings.json

I'm using appsettings.json to configure Serilog. I added Serilog.Exceptions, which works like a charm.
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Settings.Configuration", "Serilog.Exceptions" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": {
"path": "%TEMP%\\Logs\\serilog-configuration-sample.txt",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception} {Properties:j}",
"rollingInterval": "Day"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithExceptionDetails" ]
}
But since I'm using Sql Server and EF Core, the documentation says I also have to configure Serilog.Exceptions.SqlServer and Serilog.Exceptions.EntityFrameworkCore.
According to this documentation this has to be added to the enrichter. In C# code this would be:
.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
.WithDefaultDestructurers()
.WithDestructurers(new[] { new SqlExceptionDestructurer() }))
I can't figure out how to do that in appsettings.json. Can anyone help me with that?
This isn't possible at the moment, although the author is open for a PR if someone implements it.
See this issue, this discussion and this related question.

Resources