在docker下运行API时,Kibana中没有日志

时间:2019-07-07 11:28:43

标签: docker elasticsearch asp.net-core kibana serilog

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

有我的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:9200lett-api索引也会出现并记录日志。

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

谢谢!

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

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}

1 个答案:

答案 0 :(得分:1)

未将日志写入kibana,因为lett-api不在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