You want to Start with Elasticssearch, Kibana and ASP.NET Core and also want to do it fast? Let’s use Docker and find out how easy it can be:

Create a folder for your new project


Open a command prompt an run

1mkdir aspnet.elk.sample
2cd aspnet.elk.sample

Create a new ASP.NET Core project


1dotnet new mvc

Add the following Serilog packages


You´ll send the logs to ElasticSearch using Serilog:

1dotnet add package Serilog -v 2.4.0
2dotnet add package Serilog.Extensions.Logging -v 1.4.0
3dotnet add package Serilog.Sinks.ElasticSearch -v 5.1.0
4dotnet restore

Replace the contents of the Startup.cs file


Lines 27 and 66 are key to enable Serilog and the Elasticsearch sink:

 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Threading.Tasks;
 5using Microsoft.AspNetCore.Builder;
 6using Microsoft.AspNetCore.Hosting;
 7using Microsoft.Extensions.Configuration;
 8using Microsoft.Extensions.DependencyInjection;
 9using Microsoft.Extensions.Logging;
10using Serilog;
11using Serilog.Events;
12using Serilog.Sinks.Elasticsearch;
13
14namespace aspnet.elk.sample
15{
16    public class Startup
17    {
18        public Startup(IHostingEnvironment env)
19        {
20            var builder = new ConfigurationBuilder()
21                .SetBasePath(env.ContentRootPath)
22                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
23                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
24                .AddEnvironmentVariables();
25
26            // Create Serilog Elasticsearch logger
27            Log.Logger = new LoggerConfiguration()
28               .Enrich.FromLogContext()
29               .MinimumLevel.Debug()
30               .WriteTo.Elasticsearch().WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
31               {
32                   MinimumLogEventLevel = LogEventLevel.Verbose,
33                   AutoRegisterTemplate = true,
34               })
35               .CreateLogger();
36
37            Configuration = builder.Build();
38        }
39
40        public IConfigurationRoot Configuration { get; }
41
42        // This method gets called by the runtime. Use this method to add services to the container.
43        public void ConfigureServices(IServiceCollection services)
44        {
45            // Add framework services.
46            services.AddMvc();
47        }
48
49        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
50        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
51        {
52            // loggerFactory.AddConsole(Configuration.GetSection("Logging"));
53            loggerFactory.AddDebug();
54
55            if (env.IsDevelopment())
56            {
57                app.UseDeveloperExceptionPage();
58                app.UseBrowserLink();
59            }
60            else
61            {
62                app.UseExceptionHandler("/Home/Error");
63            }
64
65            // Add serilog
66            loggerFactory.AddSerilog();
67
68            app.UseStaticFiles();
69
70            app.UseMvc(routes =>
71            {
72                routes.MapRoute(
73                    name: "default",
74                    template: "{controller=Home}/{action=Index}/{id?}");
75            });
76        }
77    }
78}

Use Docker to start Elasticsearch and Kibana


You’ll need to add the following address range to your docker unsafe registry: 172.19.0.2:9200

Run the following commands:

1docker pull nshou/elasticsearch-kibana
2docker run -d -p 9200:9200 -p 5601:5601 nshou/elasticsearch-kibana

It will take a while but you’ll get a working Elasticsearch + Kibana installation.

Run the program and navigate

Run the program

1dotnet run

And navigate through some of the pages: http://localhost:5000

Setup Kibana and search your logs


Open http://localhost:5601 and configure the Index Pattern with the default values (Click Create at the bottom of the page).

Now click Discover on the side bar and start searching your logs. Enjoy!

Get the code and related files here: https://github.com/cmendible/dotnetcore.samples/tree/main/aspnet.elk.sample

Hope it helps!