首页 > 解决方案 > Serilog 与 .net 核心 web api 2.2

问题描述

我正在尝试使用ElasticSearchSinkfor Serilog。我有docker-compose文件要启动ElasticSearchKibana并且.NET Core 2.2 Web API.

这是inConfigure中的代码Startup.cs

  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            var elasticUri = Configuration["ElasticConfiguration:Uri"];

            var logger = new LoggerConfiguration()
                .Enrich.FromLogContext()
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(elasticUri))
                {
                    AutoRegisterTemplate = true
                });

            loggerFactory.AddSerilog();

            Log.Logger = logger.CreateLogger();

            app.UseMvc();
        }

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },

  "AllowedHosts": "*",

  "ElasticConfiguration": {
    "Uri": "http://localhost:9200/"
  }
}

我可以击中localhost:9200并且Kibana发射也很好。但是里面没有数据Kibana。我没有看到任何错误,但我期待ESand中有一些数据Kibana

控制器类是:

public class AuthenticationController : ControllerBase
    {
        private readonly ILogger<AuthenticationController> _logger;

        public AuthenticationController(ILogger<AuthenticationController> logger)
        {
            _logger = logger;
        }

        [HttpPost]
        [Route("token")]
        public async Task<IActionResult> ValidateToken([FromBody] AuthenticateRequest request)
        {
            _logger.LogInformation($"ValidateToken: request received with {request}");
            return Ok(new AuthenticateRespose
            {
                Token = "testing", Duration = 0
            });
        }
    }

Postman我可以从运行或仅使用in 进行docker-compose调试来达到该端点。DockerVS

我错过了什么?

标签: dockerelasticsearchasp.net-coredocker-composeserilog

解决方案


你确定这http://localhost:9200/是正确的吗?不同的 Docker 容器是否在共享网络接口上运行?如果不是,您可能应该尝试将其更改为您在 Docker 撰写文件http://<service name>:9200/service name为 Serilog 容器提供的名称。


推荐阅读