-
Notifications
You must be signed in to change notification settings - Fork 4
/
ElasticSearchStore.cs
50 lines (39 loc) · 1.57 KB
/
ElasticSearchStore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Threading;
using System.Threading.Tasks;
using Elasticsearch.Net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace raccoonLog.Stores.ElasticSearch
{
public static class ElasticSearchStoreExtensions
{
public static void AddElasticSearchStore(this HttpLoggingBuilder builder, Action<ElasticSearchStoreOptions> configureOptions)
{
var services = builder.Services;
builder.AddStore<ElasticSearchStore>(ServiceLifetime.Singleton);
services.Configure(configureOptions);
}
}
public class ElasticSearchStore : IHttpLoggingStore
{
private readonly ElasticSearchStoreOptions _options;
private readonly ElasticLowLevelClient _client;
private readonly ILogger<ElasticSearchStore> _logger;
public ElasticSearchStore(IOptions<ElasticSearchStoreOptions> options)
{
_options = options.Value;
_client = new ElasticLowLevelClient(_options.Configuration);
}
public async Task StoreAsync(LogContext logContext, CancellationToken cancellationToken = default)
{
var client = new ElasticLowLevelClient(_options.Configuration);
var response = await client.IndexAsync<BytesResponse>(_options.Index, "1", PostData.Serializable(logContext));
if (!response.Success)
{
_logger.LogError(response.OriginalException, response.OriginalException.Message);
}
}
}
}