gpt4 book ai didi

c# - .net core 2.0 在 Kubernetes pod 控制台中进行日志记录

转载 作者:行者123 更新时间:2023-12-02 11:27:24 29 4
gpt4 key购买 nike

我在 .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");

最佳答案

您是否尝试过 DI 为强大的日志记录而构建的常见第三方包?这可能适合您的需求!下面的代码展示了 Serilog 是如何注入(inject) Program.cs 的。并可用于通过您选择的多个 channel 输出其日志(我个人在 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 的 Logging 仪表板中。

关于c# - .net core 2.0 在 Kubernetes pod 控制台中进行日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49664308/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com