【问题标题】:.net core 2.0 logging inside Kubernetes pod console.net core 2.0 在 Kubernetes pod 控制台中进行日志记录
【发布时间】:2018-09-14 19:12:59
【问题描述】:

我在 .net core 2.0 中编写了一些 Web API,并使用 Kubernetes 集群中的 docker 容器进行了部署。我正在使用以下日志记录配置,但在 Kubernetes pod 控制台中看不到任何日志。我在这里错过了什么吗?:

appsettings.json 和 appsettings.Development.json 中的日志记录部分

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

在 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();
}

其他类的登录示例:

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

【问题讨论】:

  • 在 Visual Studio 的调试模式下运行代码时是否遇到问题?
  • 我遇到了同样的事情。似乎 ILogger 不会出现在 k8s 日志记录机制中的 Kubernetes STDOUT 中。对我来说,这是在 Linux Ubuntu 容器上。
  • 本地运行Container时能否获取日志?例如。 docker run &lt;image&gt;docker logs &lt;container&gt;?
  • 我无法使用与上面发布的 .netcore 2.1/minikube 完全相同的代码来重现这一点。是否有可重现的示例代码可供调查?

标签: c# docker kubernetes .net-core


【解决方案1】:

您是否尝试过 DI 为强大的日志记录而构建的常见第三方包?这可能适合您的需求!下面的代码显示了 Serilog 如何注入 Program.cs 并且可用于通过您选择的多个通道输出其日志(我个人在 macOS 上本地使用 minikube 以及 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);
                })

上述所需的输出看起来类似于 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.

这些输出也存储在 Google Cloud 的日志记录仪表板中。

【讨论】:

    【解决方案2】:

    您是否尝试在 appsettings 的 Console 键下添加 "IncludeScopes": true

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

    我能够流式传输运行此 kubectl 命令的日志:

    kubectl 日志 --follow -c [containername] [podname]

    来自正在运行的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 2018-02-27
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 2018-01-28
      相关资源
      最近更新 更多