当在docker下运行API时,Kibana中没有日志 [英] There are no logs in Kibana when API is run under docker

查看:150
本文介绍了当在docker下运行API时,Kibana中没有日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好时间.我想在Kibana中查看我的日志.为了查看它们,我使用Serilog并在docker中运行我的应用程序Elasticsearch和Kibana.不幸的是,日志未在Kibana中显示.我也找不到lett-api kibana索引.

Good time. I'd like to see my logs in Kibana. In order to see them, I use Serilog and run my app, Elasticsearch and Kibana in docker. Unfortunately logs are not appeared in Kibana. Also I can't find lett-api kibana index.

有我的Program文件:

public class Program
    {
        public static int Main(string[] args)
        {
            CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-GB");
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Verbose()
                .Enrich.FromLogContext()
                .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                .Enrich.WithProperty("app", "Lett.Api")
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(
                    new Uri("http://elasticsearch:9200"))
                {
                    AutoRegisterTemplate = true,
                    IndexFormat = "lett-api",
                    FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
                    EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
                                       EmitEventFailureHandling.WriteToFailureSink |
                                       EmitEventFailureHandling.RaiseCallback,
                    FailureSink = new FileSink("./failures.txt", new JsonFormatter(), null)
                })
                .CreateLogger();


            try
            {
                BuildWebHost(args).Run();
                return 0;
            }
            finally
            {
                Log.CloseAndFlush();
            }

        }

        private static IWebHost BuildWebHost(string[] args)
        {
            return new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseSerilog()
                .ConfigureAppConfiguration((ctx, builder) =>
                {
                    builder
                        .SetBasePath(ctx.HostingEnvironment.ContentRootPath)
                        .AddJsonFile("appsettings.json", true)
                        .AddEnvironmentVariables("Docker:");
                })
                .UseStartup<Startup>()
                .Build();
        }
    }

我的docker-compose文件:

 version: '3.7'

services: 
  postgres:
    container_name: postgresql
    image: postgres:alpine
    environment:
      - POSTGRES_PASSWORD=12345
      - POSTGRES_USER=postgres
    ports:
      - 5432:5432

  api:
    container_name: lett-api
    image: lett:latest
    restart: on-failure
    build:
      context: .
      dockerfile: ./Lett.Api.Dockerfile
    depends_on:
      - postgres
      - elasticsearch
    ports:
      - 5000:80
    environment:
      Docker:ConnectionString: "Host=postgres;Username=postgres;Password=12345;Database=Lett"

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.2.0
    container_name: elasticsearch
    environment:
      - node.name=elasticsearch
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - discovery.zen.minimum_master_nodes=1
      - discovery.type=single-node
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - "9200:9200"
    volumes:
      - elasticsearch-data:/usr/share/elasticsearch/data
    networks:
      - docker-network

  kibana:
    image: docker.elastic.co/kibana/kibana:7.2.0
    container_name: kibana
    depends_on:
      - elasticsearch
    environment:
      elasticsearch.url: "http://elasticsearch:9200"
      elasticsearch.hosts: "http://elasticsearch:9200"
      xpack.security.enabled: "false"
      xpack.monitoring.enabled: "false"
      xpack.ml.enabled: "false"
      xpack.graph.enabled: "false"
      xpack.reporting.enabled: "false"
      xpack.grokdebugger.enabled: "false"
    ports:
      - "5601:5601"
    networks:
      - docker-network

volumes:
  elasticsearch-data:
    driver: local

networks:
  docker-network:
    driver: bridge

但是,当我在本地运行我的应用程序(使用ElasticsearchUri = http://localhost:9200)时,lett-api索引也会出现并记录日志.

BUT when I run my application locally (use ElasticsearchUri = http://localhost:9200) lett-api index is appeared and logs too.

有人知道热点是什么问题吗?

Does anyone know hot what the problem is?

谢谢!

更新,我检查了docker输出并找到了以下内容:

UPDATE I've checked docker output and found the following:

lett-api         | Unable to submit event {HostingRequestStartingLog:l}
lett-api         | Unable to submit event {HostingRequestFinishedLog:l}
lett-api         | Unable to submit event {HostingRequestStartingLog:l}
lett-api         | Unable to submit event {HostingRequestFinishedLog:l}
lett-api         | Unable to submit event {HostingRequestStartingLog:l}
lett-api         | Unable to submit event CORS policy execution successful.
lett-api         | Unable to submit event Route matched with {RouteData}. Executing controller action with signature {MethodInfo} on controller {Controller} ({AssemblyName}).
lett-api         | Unable to submit event Executing action method {ActionName} - Validation state: {ValidationState}
lett-api         | Unable to submit event Executed action method {ActionName}, returned result {ActionResult} in {ElapsedMilliseconds}ms.
lett-api         | Unable to submit event Executing ObjectResult, writing value of type '{Type}'.
lett-api         | Unable to submit event Executed action {ActionName} in {ElapsedMilliseconds}ms
lett-api         | Unable to submit event {HostingRequestFinishedLog:l}

推荐答案

未将日志写入kibana,因为lett-api不在docker-network中.

Logs were not written to kibana because lett-api wasn't in docker-network.

有正确的docker-compose文件:

version: '3.7'

services: 
  postgres:
    container_name: postgresql
    image: postgres:alpine
    environment:
      - POSTGRES_PASSWORD=12345
      - POSTGRES_USER=postgres
    ports:
      - 5432:5432

  api:
    container_name: lett-api
    image: lett:latest
    restart: on-failure
    build:
      context: .
      dockerfile: ./Lett.Api.Dockerfile
    depends_on:
      - postgres
      - elasticsearch
    ports:
      - 5000:80
    environment:
      Docker:ConnectionString: "Host=postgres;Username=postgres;Password=12345;Database=Lett"
    networks:
      - docker-network

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.2.0
    container_name: elasticsearch
    environment:
      - node.name=elasticsearch
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - discovery.zen.minimum_master_nodes=1
      - discovery.type=single-node
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - "9200:9200"
    volumes:
      - elasticsearch-data:/usr/share/elasticsearch/data
    networks:
      - docker-network

  kibana:
    image: docker.elastic.co/kibana/kibana:7.2.0
    container_name: kibana
    depends_on:
      - elasticsearch
    environment:
      elasticsearch.url: "http://elasticsearch:9200"
      elasticsearch.hosts: "http://elasticsearch:9200"
      xpack.security.enabled: "false"
      xpack.monitoring.enabled: "false"
      xpack.ml.enabled: "false"
      xpack.graph.enabled: "false"
      xpack.reporting.enabled: "false"
      xpack.grokdebugger.enabled: "false"
    ports:
      - "5601:5601"
    networks:
      - docker-network

volumes:
  elasticsearch-data:
    driver: local

networks:
  docker-network:
    driver: bridge

这篇关于当在docker下运行API时,Kibana中没有日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆