Introduction
Logging is an important aspect of any application as it helps in troubleshooting, monitoring, and analyzing the behavior of the application. In this blog post, we will explore how to configure logging in an ASP.NET Core application to store logs in Elasticsearch and visualize them using Kibana.
Prerequisites
Before we proceed, make sure you have the following prerequisites installed:
- Visual Studio or Visual Studio Code
- .NET Core SDK
- Elasticsearch and Kibana installed and running locally or on a remote server
Setting up Elasticsearch and Kibana
-
Download and install Elasticsearch from the official website, and make sure it is running on the default port
9200
. -
Download and install Kibana from the official website, and make sure it is running on the default port
5601
. -
Open your web browser and navigate to
http://localhost:5601
to access the Kibana dashboard. Here, you can perform various operations like creating indices, defining mappings, and visualizing your logs.
Configuring ASP.NET Core Logging
To begin configuring logging in your ASP.NET Core application, follow the steps below:
-
Create a new ASP.NET Core project in Visual Studio or Visual Studio Code.
-
Open the
appsettings.json
file and add the following configuration settings for Elasticsearch:
"Logging": {
"Elasticsearch": {
"Uri": "http://localhost:9200",
"Index": "myapp-logs"
}
}
-
Install the following NuGet packages to add the required dependencies:
Serilog.AspNetCore
Serilog.Sinks.Elasticsearch
-
In the
Program.cs
file, configure the logger by adding the following code:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
logging.ClearProviders();
logging.AddSerilog(new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Elasticsearch(
new ElasticsearchSinkOptions(new Uri(Configuration["Logging:Elasticsearch:Uri"]))
{
AutoRegisterTemplate = true,
IndexFormat = Configuration["Logging:Elasticsearch:Index"]
})
.CreateLogger());
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
- Run the application, and you should see logs being generated in Elasticsearch.
Visualizing Logs using Kibana
Once you have your logs stored in Elasticsearch, you can use Kibana to create visualizations and dashboards. Follow the steps below to create a simple visualization:
-
Access the Kibana dashboard by navigating to
http://localhost:5601
in your web browser. -
Click on the "Discover" tab in Kibana, and you should see your logs listed.
-
To create a visualization, click on the "Visualize" tab and select the type of visualization you want to create (e.g., Line Chart, Pie Chart, etc.).
-
Configure the visualization by selecting the appropriate log field, aggregation type, and any additional filters or settings.
-
Save the visualization and add it to a Kibana dashboard for easy access and monitoring.
Conclusion
In this blog post, we have explored how to configure logging in an ASP.NET Core application to store logs in Elasticsearch and visualize them using Kibana. By following these steps, you can gain valuable insights into your application's behavior and easily monitor and troubleshoot any issues that arise.
本文来自极简博客,作者:绿茶味的清风,转载请注明原文链接:ASP.NET Core Logging in Elasticsearch with Kibana