我在docker容器中运行了.net核心应用程序,也有另一个容器具有elasticsearch端口:9201和kibana端口:5602。当我在本地运行示例.net核心应用程序时,日志直接保存在elasticsearch上,但是在另一个dockerized .net核心应用程序中,相同的serilog日志显示在docker容器日志中。我添加了示例代码。对于任何帮助,我非常感谢您确实需要此帮助。 appsettings.json(与docker和development相同):
"Logging": {
"LogLevel": {
"Default": "Warning",
"System": "Warning",
"Microsoft": "Warning"
}}, "ElasticConfiguration": {
"Uri": "http://localhost:9001" },"AllowedHosts": "*"
startup.cs:
var builder = new ConfigurationBuilder()
.SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", reloadOnChange: true, optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
var elasticUri = Configuration["ElasticConfiguration:Uri"];
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(elasticUri))
{
AutoRegisterTemplate = true,
})
.CreateLogger();
配置:
loggerFactory.AddSerilog();
在控制器中:
ILogger<Manager> _logger;
public Manager(ILogger<Manager> logger)
{ _logger = logger;}
}
_logger.LogWarning("Stream Has Started");