【发布时间】: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 <image>和docker logs <container>? -
我无法使用与上面发布的 .netcore 2.1/minikube 完全相同的代码来重现这一点。是否有可重现的示例代码可供调查?
标签: c# docker kubernetes .net-core