Zero.Logging.Elasticsearch 1.0.0-alpha3-20180228

This is a prerelease version of Zero.Logging.Elasticsearch.
There is a newer version of this package available.
See the version list below for details.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Zero.Logging.Elasticsearch --version 1.0.0-alpha3-20180228
NuGet\Install-Package Zero.Logging.Elasticsearch -Version 1.0.0-alpha3-20180228
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Zero.Logging.Elasticsearch" Version="1.0.0-alpha3-20180228" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Zero.Logging.Elasticsearch --version 1.0.0-alpha3-20180228
#r "nuget: Zero.Logging.Elasticsearch, 1.0.0-alpha3-20180228"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Zero.Logging.Elasticsearch as a Cake Addin
#addin nuget:?package=Zero.Logging.Elasticsearch&version=1.0.0-alpha3-20180228&prerelease

// Install Zero.Logging.Elasticsearch as a Cake Tool
#tool nuget:?package=Zero.Logging.Elasticsearch&version=1.0.0-alpha3-20180228&prerelease

Logging in Elasticsearch with Kibana

使用Docker部署Elasticsearch和Kibana

ELKstack是Elasticsearch、Logstash、Kibana三个开源软件的组合,是当今最为流行的统一日志分析平台。对于它们的介绍,网上非常之多,这里就不再多说。

在本文中只使用了ElasticsearchKibana,前者是分布式搜索系统,后者是一个可视化平台,使用docker来部署非常简单:

部署Elasticsearch

如下,绑定端口9200,并将容器命名为elasticsearch

docker run --name=elasticsearch -d -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" docker.elastic.co/elasticsearch/elasticsearch:6.2.2

然后在浏览器中打开 http://localhost:9200/,输出如下:

{
	"name": "qFQvLqr",
	"cluster_name": "docker-cluster",
	"cluster_uuid": "bdc5YhZlQHu0mCN7acNKBw",
	"version": {
		"number": "6.2.2",
		"build_hash": "10b1edd",
		"build_date": "2018-02-16T21:01:30.685723Z",
		"build_snapshot": false,
		"lucene_version": "7.2.1",
		"minimum_wire_compatibility_version": "5.6.0",
		"minimum_index_compatibility_version": "5.0.0"
	},
	"tagline": "You Know, for Search"
}

部署Kibana

Kibana的部署依赖于Elasticsearch:

docker run --name=kibana --link=elasticsearch -d -p 5601:5601 docker.elastic.co/kibana/kibana:6.2.2

主要注意的是,在这里使用了--link=elasticsearch来链接到elasticsearch容器,如果要使用外部的elasticsearch服务,可以使用-e "elasticsearch.url=http://changeme:9200"来指定。

然后在浏览器中打开 http://localhost:5601/

kibana_empty

如上,部署成功,不过还没有任何数据。

使用docker-compose部署

当需要部署多个相关的服务时,更加推荐使用docker-compose来部署:

首先,我们创建一个docker-compose.yml文件:

version: '3'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:${TAG}
    container_name: elasticsearch
    environment:
      - http.host=0.0.0.0
      - transport.host=127.0.0.1
      - ELASTICSEARCH_PASSWORD=${ELASTIC_PASSWORD}
    ports:
      - 9200:9200
    networks: 
      - stack

  kibana:
    image: docker.elastic.co/kibana/kibana:${TAG}
    container_name: kibana
    environment:
      - ELASTICSEARCH_USERNAME=kibana
      - ELASTICSEARCH_PASSWORD=${ELASTIC_PASSWORD}
    ports: 
      - 5601:5601
    networks: 
      - stack
    depends_on: 
      - elasticsearch

networks:
  stack:
    driver: bridge

如上,我们定义了TAGELASTIC_PASSWORD两个环境变量,方便在部署时候灵活的指定版本号和密码。

为方便测试部署,我们可以定义一个默认的环境变量文件.env

TAG=6.2.2
ELASTIC_PASSWORD=Qwer1234

然后,直接运行如下命令即可:

docker-compose up

接下来,将日志写入到Elasticsearch。

记录日志到Elasticsearch

我们创建一个 ASP.NET Core WebApi 项目,添加如下Package:

dotnet add package Zero.Logging.Elasticsearch --version 1.0.0-alpha3-20180228

添加ElasticsearchProvider

然后在Program.cs文件中使用AddElasticsearch扩展方法为日志系统添加ElasticsearchProvider

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddElasticsearch();
        })
        .UseStartup<Startup>()
        .Build();

记录日志

对于日志的记录则不需要任何的修改:

public class ValuesController : Controller
{
    private readonly ILogger _logger;

    public ValuesController(ILogger<ValuesController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public void Get()
    {
        _logger.LogTrace("Log Trace.");
        _logger.LogInformation("Log Information.");
        _logger.LogDebug("Log Debug.");
        try
        {
            throw new Exception("Boom");
        }
        catch (Exception ex)
        {
            _logger.LogCritical(1, ex, "Unexpected critical error starting application");
            _logger.LogError(1, ex, "Unexpected error");
            _logger.LogWarning(1, ex, "Unexpected warning");
        }
    }
}

在Kibana查看

刷新浏览器,显示如下:

kibana_initdata

在Index pattern中输入logstash-*,点击下一步:

kibana_index

如上,选择timestamp,创建索引,最终显示如下:

kinaba_discover

配置

如上一行代码,零配置完成Elasticsearch的写入,默认使用的Elasticsearch地址为http://localhost:9200,如果我们需要额外的配置也很简单,有如下两种方式:

使用配置文件进行配置

appsettings.json中添加如下配置:

{
  "Logging": {
    "IncludeScopes": false,
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Elasticsearch": {
      "LogLevel": {
        "Default": "Information"
      },
      "ElasticsearchUrl": "http://changeme:9200",
      "AutoRegisterTemplate": true
    }
  }
}
使用代码进行配置
WebHost.CreateDefaultBuilder(args)
    .ConfigureLogging((hostingContext, logging) =>
    {
        logging.AddFile().AddElasticsearch(o =>
        {
            o.PipelineName = "http://changeme:9200";
            o.AutoRegisterTemplate = true;
        });
    })

需要注意,如果使用代码的方式进行配置,则配置文件中的配置不再生效。

更多的配置信息参见 EsLoggerOptions

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Zero.Logging.Elasticsearch:

Package Downloads
LY.Common

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.1 16,121 12/14/2018
2.2.0 746 12/5/2018
2.1.0 1,091 10/22/2018