前言
最近園子裡看到一篇<.Net Core with 微服務 - Elastic APM> 的文章(主要用於對介面的呼叫鏈、效能進行監控),非常實用,這裡講解.NetCore將日誌寫入ElasticSearch,並用Kibana搜尋日誌
部署環境
centos
依賴環境
docker
docker-compose
.net core 3.1
使用部署
1.建立 docker-compose.yml 檔案
注意事項:
- 設定es記憶體,java程式一般很吃記憶體,根據伺服器配置進行調優 - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- kibana漢化(7.0以上版本),根據個人情況決定 - output.i18n.locale="zh-CN"
- 安裝apm_server,這個模組非必須項,不需要的可以去掉,感興趣可以參考 <.Net Core with 微服務 - Elastic APM>
- 拉取映象失敗,由於網路原因,可能存在拉取映象失敗,網路較差建議單獨拉取映象,再執行
version: '3.1'
services:
elasticsearch:
container_name: elasticsearch
hostname: elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2
restart: always
ports:
- 9200:9200
- 9300:9300
volumes:
- elasticsearch-data:/usr/share/elasticsearch/data
environment:
- xpack.monitoring.enabled=true
- xpack.watcher.enabled=false
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- discovery.type=single-node
kibana:
container_name: kibana
hostname: kibana
image: docker.elastic.co/kibana/kibana:7.9.2
restart: always
ports:
- 5601:5601
depends_on:
- elasticsearch
environment:
- ELASTICSEARCH_URL=http://localhost:9200
apm_server:
image: docker.elastic.co/apm/apm-server:7.9.2
restart: always
container_name: apm_server
hostname: apm_server
command: --strict.perms=false -e
environment:
- output.elasticsearch.hosts=["localhost:9200"]
- output.i18n.locale="zh-CN"
ports:
- 8200:8200
depends_on:
- kibana
- elasticsearch
volumes:
elasticsearch-data:
2.執行 docker-compose.yml 檔案
注意事項:
- 容器啟動後,kibana需要一段時間初始化
- 本次需要開放的埠 es:9200,9300 kibana:5601 apm-server:8200
docker-compose up -d //構建啟動容器 -d 後臺執行
docker-compose down //停止up 命令所啟動的容器,並移除網路
docker-compose stop //停止容器
docker-compose start //啟動容器
docker-compose restart //重啟容器
3.啟動kibana web介面,瀏覽器輸入:http://[ip]:5601/app/home#/ [ip]為伺服器ip地址或域名
- 能正常的開啟介面,則ElasticSearch、Kibana、Apm 部署完成
4.使用 ASP.NET Core 和 Serilog 記錄到 Elasticsearch
1.新建一個.net core web程式(.net core3.1)
2.將以下 Serilog 包新增到專案中
- Serilog.AspNetCore
- Serilog.Enrichers.Environment
- Serilog.Sinks.Debug
- Serilog.Sinks.ElasticSearch
3.刪除 appsettings.json 中的 Logging 部分並將其替換為以下配置,如果是dev環境,則修改appsettings.Development.json檔案
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information",
"System": "Warning"
}
}
},
"ElasticConfiguration": {
"Uri": "http://localhost:9200" //這裡填寫es地址
},
"AllowedHosts": "*"
}
4.在 Program.cs 中配置登入
注意在es中建立的索引,這裡的索引是it-tibos-api,可以根據業務進行修改
public class Program
{
public static void Main(string[] args)
{
//configure logging first
ConfigureLogging();
//then create the host, so that if the host fails we can log errors
CreateHost(args);
}
private static void ConfigureLogging()
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile(
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
optional: true)
.Build();
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.Enrich.WithMachineName()
//.WriteTo.Debug()
.WriteTo.Console()
.WriteTo.Elasticsearch(ConfigureElasticSink(configuration, environment))
.Enrich.WithProperty("Environment", environment)
.ReadFrom.Configuration(configuration)
.CreateLogger();
}
private static ElasticsearchSinkOptions ConfigureElasticSink(IConfigurationRoot configuration, string environment)
{
return new ElasticsearchSinkOptions(new Uri(configuration["ElasticConfiguration:Uri"]))
{
AutoRegisterTemplate = true,
IndexFormat = $"it-tibos-api" //索引
};
}
private static void CreateHost(string[] args)
{
try
{
CreateHostBuilder(args).Build().Run();
}
catch (System.Exception ex)
{
Log.Fatal($"Failed to start {Assembly.GetExecutingAssembly().GetName().Name}", ex);
throw;
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureAppConfiguration(configuration =>
{
configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
configuration.AddJsonFile(
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
optional: true);
})
.UseSerilog();
}
5.在action裡記錄日誌,並執行程式
_logger.LogInformation($"這是一條測試日誌,傳送時間{DateTime.Now}");
_logger.LogWarning($"這是一條測試警告日誌,傳送時間{DateTime.Now}");
try
{
throw new Exception("Some bad code was executed");
}
catch (Exception ex)
{
_logger.LogError($"這是一條異常日誌,傳送時間{DateTime.Now}");
}
6.在Kibana新增ElasticSearch索引
7.搜尋日誌
8.管理索引