TransWikia.com

.net core 2.0 logging inside Kubernetes pod console

Stack Overflow Asked by Abhay on December 30, 2021

I wrote few web APIs in .net core 2.0 and deployed it using a docker container inside a Kubernetes cluster. I am using the below logging configuration but can’t see any logs inside the Kubernetes pod console. Am I missing something here?:

Logging section in appsettings.json & appsettings.Development.json

{
  "Logging": {
    "IncludeScopes": true,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
    "Console": {
      "LogLevel": {
        "Default": "Information",
        "System": "Information",
        "Microsoft": "Information"
      }
    }
  }
}

Inside Program.cs:

public static IWebHost BuildWebHost(string[] args)
{
    return new WebHostBuilder()
        .UseKestrel()
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            var env = hostingContext.HostingEnvironment;

            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

            if (env.IsDevelopment())
            {
                var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
                if (appAssembly != null)
                {
                    config.AddUserSecrets(appAssembly, optional: true);
                }
            }

            config.AddEnvironmentVariables();

            if (args != null)
            {
                config.AddCommandLine(args);
            }
        })
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
            logging.AddConsole();
            logging.AddDebug();
        })
        .UseDefaultServiceProvider((context, options) =>
        {
            options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
        })
        .UseStartup<Startup>()
        .Build();
}

Example of logging in other classes:

_logger.LogInformation("This log should go in kubernetes pod console");

One Answer

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.FileSystemXmlRepository
Storing 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.XmlKeyManager
No XML encryptor configured. Key {xxxxxx} may be persisted to storage in unencrypted form.

[2020-08-04 12:11:37 Warning] Microsoft.AspNetCore.Server.Kestrel
Overriding address(es) 'https://+:8081'. Binding to endpoints defined in UseKestrel() instead.

Hosting environment: Production
Content root path: /app
Now listening on: https://0.0.0.0:8081
Application started. Press Ctrl+C to shut down.

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

Answered by Nicholas on December 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP