I have my dotnet core application set up to use SeriLog which logs to an ElasticSearch Sink. When running my dotnet core app locally with ElasticSearch and Kibana I have two separate containers I'm able to log stuff from my application to Elastic and I'm also able to see these log messages in Kibana.
When I include my dotnet core application in the docker-compose.yml file, navigating to the endpoint where the app is located, I expect it to log several messages to Elasticsearch, but it doesn't. I suspect that my application doesn't understand how to communicate with Elasticsearch when it's being containerized. My docker-compose.yml looks like this:
version: '3.0'
services:
db:
image: mysql:5.7
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 1
MYSQL_DATABASE: chtr
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbuserpassword
volumes:
- dbdata:/var/lib/mysql
- ./_MySQL_Init_Script:/docker-entrypoint-initdb.d
restart: always
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4
container_name: elasticsearch
ports:
- "9200:9200"
volumes:
- elasticsearch-data:/usr/share/elasticsearch/data
kibana:
image: docker.elastic.co/kibana/kibana:6.2.4
container_name: kibana
ports:
- "5601:5601"
depends_on:
- elasticsearch
chtr.server:
depends_on:
- db
- kibana
image: trebias/chtr.server
build:
context: .
ports:
- "8080:80"
- "56:5601"
volumes:
dbdata:
elasticsearch-data:
chtr.server is my image being pull from my docker hub.
Over to the appsettings.json within my dotnet core application:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"ElasticConfiguration": {
"Uri": "http://[::]:9200/"
}
}
Where I create the Logger like this in my Startup.cs file:
var elasticSearch = Configuration["ElasticConfiguration:Uri"];
Log.Logger = new LoggerConfiguration().Enrich.FromLogContext().WriteTo.Elasticsearch(new ElasticsearchSinkOptions(
new Uri(elasticSearch)) { AutoRegisterTemplate = true }).CreateLogger();
Like I said at the beginning of this post; It works when I run Kibana and Elasticsearch in containers and my app locally, but not when all three apps are in containers.
Any suggestions?
The service is running under container that's why you should write your log using the container name like bellow:
Appsettings.Development.json:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"ElasticConfiguration": {
"Uri": "http://elasticsearch:9200/"
}
}
You can find here a full example showing how to setup Serilog, Seq, elasticsearch and kibana to work together under Docker containers solution.
structure logging with serilog seq and elastic search under docker