Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core 2.0 logging inside Kubernetes pod console

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"); 
like image 243
Abhay Avatar asked Apr 05 '18 04:04

Abhay


People also ask

How do I check the logs in my pod?

Checking the logs of a crashed pod In case that a pod restarts, and you wanted to check the logs of the previous run, what you need to do is to use the --previous flag: kubectl logs nginx-7d8b49557c-c2lx9 --previous.

How do I check logs in Kubernetes dashboard?

Viewing Pod Logs A common use of the dashboard is monitoring live log output of Pods and Jobs. Find the item you need to inspect in one of the dashboard's resource tables. Click the right-most three dots icon, then select the “Logs” item from the menu.

How do you debug a pod in Kubernetes?

You can instead add a debugging container using kubectl debug . If you specify the -i / --interactive argument, kubectl will automatically attach to the console of the Ephemeral Container. This command adds a new busybox container and attaches to it.


1 Answers

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.

like image 197
Nicholas Avatar answered Sep 23 '22 13:09

Nicholas