从我的 asp.net core webapi 到我的 dockerized elasticsearch 没有生成 Serilog 日志

问题描述 投票:0回答:1
c# docker-compose serilog serilog-sinks-elasticsearch
1个回答
0
投票

就我而言,appsettings.Development.json 中有“nodeUris”:“http://localhost:9200”。从 Visual Studio 运行应用程序时效果很好,但当两个容器都在 Docker 上运行时,它无法连接到 Elasticsearch。

解决方案是像这样覆盖环境变量:

myapi:
  environment:
    - Serilog__WriteTo__2__Args__nodeUris=http://elasticsearch:9200

数字表示Elasticsearch在“WriteTo”数组中的位置。

对于调试此问题非常有用的是通过在应用程序的最开头添加以下内容来启用 Serilog SelfLog:

Serilog.Debugging.SelfLog.Enable(msg =>
{
    try
    {
        if (!File.Exists("internallog.log"))
        {
            File.Create("internallog.log").Close();
        }

        File.AppendAllText("internallog.log", msg, Encoding.UTF8);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.