记录(通过Serilog)到ElasticSearch不起作用

时间:2018-02-22 11:30:37

标签: c# elasticsearch logging kibana serilog

我正在尝试将日志发送到ElasticSearch并在Kibana中查看它们。由于一些奇怪的原因,它不起作用:项目正在做一些事情,但Kibana没有任何东西。我下载并尝试了10多个与此主题相关的项目,但是他们被引入作为工作样本,他们没有完成工作:Kibana没有任何东西!

我在这里发布其中一个的源代码:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        // Create Serilog Elasticsearch logger
        Log.Logger = new LoggerConfiguration()
           .Enrich.FromLogContext()
           .MinimumLevel.Information() //debug
           .WriteTo.Elasticsearch().WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
           {
               MinimumLogEventLevel = LogEventLevel.Verbose,
               AutoRegisterTemplate = true
           })
           .CreateLogger();

        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();


        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });


    }
}

这是控制器:

public IActionResult Index()
{
    _logger.LogInformation("Hello world!");
    return View();
}

public IActionResult About()
{
    var elasticUri = new Uri("http://localhost:9200/cmstest/obj");

    var loggerConfig = new LoggerConfiguration()
                    .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(elasticUri)
                    {
                        AutoRegisterTemplate = true,
                        MinimumLogEventLevel = Serilog.Events.LogEventLevel.Verbose
                    });


    var logger = loggerConfig.CreateLogger();

    logger.Error("Hello world");
    logger.Information("Hello world");
    logger.Warning("Hello world");

    return View();
}




public IActionResult Contact()
{
    _logger.LogInformation("Hello world");

    var logger = new LoggerConfiguration()
                    .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(
                    new Uri("http://localhost:9200"))
                    {
                        AutoRegisterTemplate = true
                    }).CreateLogger();

    logger.Information("Hello world");

    return View();
}

PS 我还发现log4net的例子 - 既不能正常工作

0 个答案:

没有答案