Abh*_*hay 9 c# docker kubernetes .net-core
我在.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"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
内部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();
}
Run Code Online (Sandbox Code Playgroud)
登录其他类的示例:
_logger.LogInformation("This log should go in kubernetes pod console");
Run Code Online (Sandbox Code Playgroud)
您是否尝试过使用 DI 为强大的日志记录而构建的通用第三方包?这可能适合您的需求!下面的代码显示了如何注入 Serilog Program.cs,并可用于通过您选择的多个通道输出其日志(我个人在 macOS 上本地使用 minikube 以及 GCP 上的暂存环境)。
WebHost.CreateDefaultBuilder(args)\n .UseSerilog((context, configuration) =>\n {\n configuration\n .MinimumLevel.Debug()\n .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)\n .MinimumLevel.Override("System", LogEventLevel.Warning)\n .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)\n .Enrich.FromLogContext()\n .WriteTo.Console(\n outputTemplate:\n "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}",\n theme: AnsiConsoleTheme.Literate);\n })\nRun Code Online (Sandbox Code Playgroud)\n上面的期望输出类似于 Kubernetes 中的输出:
\n xxxxx@iMac \xee\x82\xb0 ~/Projects/xxxxx \xee\x82\xb0 \xee\x82\xa0 xxxxbranch/xxx-xxx \xee\x82\xb0 kubectl logs xxxx-xxxx-6b9dd8dc67-vc9ch\n[2020-08-04 12:11:37 Warning] Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository\nStoring 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.\n\n[2020-08-04 12:11:37 Warning] Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager\nNo XML encryptor configured. Key {xxxxxx} may be persisted to storage in unencrypted form.\n\n[2020-08-04 12:11:37 Warning] Microsoft.AspNetCore.Server.Kestrel\nOverriding address(es) 'https://+:8081'. Binding to endpoints defined in UseKestrel() instead.\n\nHosting environment: Production\nContent root path: /app\nNow listening on: https://0.0.0.0:8081\nApplication started. Press Ctrl+C to shut down.\nRun Code Online (Sandbox Code Playgroud)\n这些输出也存储在 Google Cloud 的日志记录仪表板中。
\n| 归档时间: |
|
| 查看次数: |
693 次 |
| 最近记录: |