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

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"]

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}"
}
}
]
}
}

VSCode multiple docker run task recognize only the last docker run task

I have 2 tasks in visual studio code to run 2 different images into containers. Only the last docker run task is recognized by vscode.
This is my tasks.json file
{
"version": "2.0.0",
"tasks": [
{
"label": "docker-build-1",
"type": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "image1:latest",
"dockerfile": "${workspaceFolder}/app1/dev.Dockerfile",
"context": "${workspaceFolder}/",
"pull": true
}
},
{
"label": "docker-build-2",
"type": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "image2:latest",
"dockerfile": "${workspaceFolder}/app2/dev.Dockerfile",
"context": "${workspaceFolder}/",
"pull": true
}
},
{
"label": "docker-run-1",
"type": "docker-run",
"dependsOn": [
"docker-build-1"
],
"python": {
"module": "app.main"
},
"dockerRun": {
"network": "mynetwork"
}
},
{
"label": "docker-run-2",
"type": "docker-run",
"dependsOn": [
"docker-build-2"
],
"python": {
"module": "app.main"
},
"dockerRun": {
"network": "mynetwork"
}
},
]
}
When vscode shows the menu for running task, only thask docker-run-2 is showing:
Actually, only the last docker run task in the tasks.json file is shown. If I change the order in the list of tasks, then vscode only recognize docker-run-1. I searched in the documentation and it doesn't says anything about this behaviour. Any idea why this is happening? The idea is to setup 2 debug configurations in vscode for the 2 apps, but running the debug config for the app that is not the last produce an error in vscode:
Came across this same issue today. Seems that the "dockerRun" attribute between the run tasks has to be different. In my case i just added a test environment variable to one of the tasks and then both started to appear in the task list.

asp net core docker serilog configuration

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.

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