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

标签 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:9200)lett-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}

最佳答案

未将日志写入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

关于docker - 在docker下运行API时,Kibana中没有日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56921743/

相关文章:

mongodb - Docker撰写镜像文件夹

elasticsearch - 创建 ElasticSearch 域时出错 : ValidationException: Authentication error

ElasticSearch 日期字段映射错误

asp.net-core - asp-for 标记在 asp.net 核心中的复选框上添加必填字段验证

windows - 尝试使用 Install-Module 在 Windows Server 2016 中安装 Docker 时出错

database - 使用相同的数据/源代码同步多个服务器和本地机器

node.js - 为什么docker镜像太大? NodeJS 应用程序

spring-boot - Junit测试因Elasticsearch而失败

c# - 我应该使用哪个 ReThinkDB .NET 驱动程序?它们之间的功能差异是什么?

.net - 本地 azure 服务结构上的证书为空错误