简体   繁体   中英

.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 Kube.netes cluster. I am using the below logging configuration but can't see any logs inside the Kube.netes 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");

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.

Did you try adding "IncludeScopes": true under the Console key in your appsettings?

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

I was able to stream logs running this kubectl command:

kubectl logs --follow -c [containername] [podname]

Reponse from running pod:

←[40m←[32minfo←[39m←[22m←[49m: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://[::]:80
←[40m←[32minfo←[39m←[22m←[49m: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
←[40m←[32minfo←[39m←[22m←[49m: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
←[40m←[32minfo←[39m←[22m←[49m: Microsoft.Hosting.Lifetime[0]
      Content root path: /app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM