就我而言,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());
}
});