踩坑-NLog Elasticsearch Target在docker里面无法记录的问题(Network unreachable)

踩坑-NLog Elasticsearch Target在docker里面无法记录的问题(Network unreachable)

421发表于2020-03-24

最近在调整技术架构,采用docker部署.net core程序,里面用到了NLog记录日志,且存储到Elasticsearch中。使用的NLog.Targets.ElasticSearch

原来配置:

appsettings.json:

from clipboard

Nlog.Config.xm:

from clipboard

一直报如下错误:

Error ElasticSearch: Failed to send log messages. status= Exception: Elasticsearch.Net.ElasticsearchClientException: Failed to ping the specified node.. Call: Status code unknown from: HEAD /
 ---> Elasticsearch.Net.PipelineException: Failed to ping the specified node.
 ---> Elasticsearch.Net.PipelineException: An error occurred trying to write the request data to the specified node.
 ---> System.Net.Http.HttpRequestException: Network unreachable
 ---> System.Net.Sockets.SocketException (101): Network unreachable
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at Elasticsearch.Net.HttpConnection.Request[TResponse](RequestData requestData)
   --- End of inner exception stack trace ---
   at Elasticsearch.Net.RequestPipeline.Ping(Node node)
   --- End of inner exception stack trace ---
   at Elasticsearch.Net.RequestPipeline.Ping(Node node)
   at Elasticsearch.Net.Transport`1.Ping(IRequestPipeline pipeline, Node node)
   at Elasticsearch.Net.Transport`1.Request[TResponse](HttpMethod method, String path, PostData data, IRequestParameters requestParameters)
   --- End of inner exception stack trace ---


看到上面Network unreachable心中一紧,难道是网络不通过。

我的docker和es都在同一台服务器里面。

通过docker exec -it 进入容器,使用curl命令试试网络情况。


curl http://ip:9200是通过的,说明网络没问题。

于是看看是不是NLog.Targets.ElasticSearch查看源码找到直正的问题。

from clipboard


从上看出配置项已经废弃了,所以我们虽然设置了es地址为对应ip地址,其实还是取到的默认值locahost。如下图:

from clipboard

那么要怎么配置这个地址呢?

from clipboard

在target结节的uri属性加上es的地址,就ok了。


===================================================

docker镜像采用的是Linux alpine版本,所以yum不能用了,只能用apk。

使用apk安装curl。


修改源:
vi /etc/apk/repositories

替换成阿里源
http://mirrors.aliyun.com/alpine/v3.8/main/
http://mirrors.aliyun.com/alpine/v3.8/community/


之后更新源
apk update

安装curl
apk add curl

小编蓝狐