【问题标题】:Serilog with .net core web api 2.2Serilog 与 .net 核心 web api 2.2
【发布时间】:2019-06-06 19:21:56
【问题描述】:

我正在尝试将ElasticSearchSink 用于Serilog。我有docker-compose 文件要启动ElasticSearchKibana.NET Core 2.2 Web API

这是ConfigureStartup.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:9200Kibana 也可以正常启动。但是Kibana中没有数据。我没有看到任何错误,但我期待 ESKibana 中有一些数据。

控制器类是:

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 到达该端点,既可以从docker-compose 运行,也可以仅在VS 中使用Docker 进行调试。

我错过了什么?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 是的,如果我使用任何浏览器,它都可以工作,不确定 .net 核心何时在 docker 中运行,是否可以访问它
    猜你喜欢
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2022-12-08
    相关资源
    最近更新 更多