.net core 2.0 logging inside Kubernetes pod console .net core 2.0 logging inside Kubernetes pod console docker docker

.net core 2.0 logging inside Kubernetes pod console


Have you attempted to DI common third-party packages built for powerful logging instead? That might suit your needs! The code below shows how Serilog is injected in Program.cs and can be used to output its logs through several channels of your choice (I'm personally using minikube locally on macOS along with a staging environment on GCP).

WebHost.CreateDefaultBuilder(args)                .UseSerilog((context, configuration) =>                {                    configuration                        .MinimumLevel.Debug()                        .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)                        .MinimumLevel.Override("System", LogEventLevel.Warning)                        .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)                        .Enrich.FromLogContext()                        .WriteTo.Console(                            outputTemplate:                            "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}",                            theme: AnsiConsoleTheme.Literate);                })

The desired output from the above would look something like that in Kubernetes:

 xxxxx@iMac  ~/Projects/xxxxx   xxxxbranch/xxx-xxx  kubectl logs xxxx-xxxx-6b9dd8dc67-vc9ch[2020-08-04 12:11:37 Warning] Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepositoryStoring keys in a directory '/xxxxxxxxx/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.[2020-08-04 12:11:37 Warning] Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManagerNo XML encryptor configured. Key {xxxxxx} may be persisted to storage in unencrypted form.[2020-08-04 12:11:37 Warning] Microsoft.AspNetCore.Server.KestrelOverriding address(es) 'https://+:8081'. Binding to endpoints defined in UseKestrel() instead.Hosting environment: ProductionContent root path: /appNow listening on: https://0.0.0.0:8081Application started. Press Ctrl+C to shut down.

These outputs are also stashed in Google Cloud's Logging dashboard.