|
| 1 | +using Elasticsearch.Net; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using Nest; |
| 4 | +using Newtonsoft.Json; |
| 5 | +using Serilog.Ui.Core; |
| 6 | +using System; |
| 7 | +using System.IO; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Serilog.Ui.ElasticSearchProvider.Extensions |
| 12 | +{ |
| 13 | + public static class SerilogUiOptionBuilderExtensions |
| 14 | + { |
| 15 | + public static void UseElasticSearchDb(this SerilogUiOptionsBuilder optionsBuilder, |
| 16 | + Uri endpoint, |
| 17 | + string indexName) |
| 18 | + { |
| 19 | + if (endpoint == null) throw new ArgumentNullException(nameof(endpoint)); |
| 20 | + if (string.IsNullOrEmpty(indexName)) throw new ArgumentNullException(nameof(indexName)); |
| 21 | + |
| 22 | + var options = new ElasticSearchDbOptions |
| 23 | + { |
| 24 | + IndexName = indexName |
| 25 | + }; |
| 26 | + |
| 27 | + var builder = ((ISerilogUiOptionsBuilder)optionsBuilder); |
| 28 | + |
| 29 | + builder.Services.AddSingleton(options); |
| 30 | + |
| 31 | + var pool = new SingleNodeConnectionPool(endpoint); |
| 32 | + var connectionSettings = new ConnectionSettings(pool, sourceSerializer: (builtin, values) => new VanillaSerializer()); |
| 33 | + |
| 34 | + builder.Services.AddSingleton<IElasticClient>(o => new ElasticClient(connectionSettings)); |
| 35 | + builder.Services.AddScoped<IDataProvider, ElasticSearchDbDataProvider>(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public class VanillaSerializer : IElasticsearchSerializer |
| 40 | + { |
| 41 | + public T Deserialize<T>(Stream stream) |
| 42 | + => (T)Deserialize(typeof(T), stream); |
| 43 | + |
| 44 | + |
| 45 | + public object Deserialize(Type type, Stream stream) |
| 46 | + { |
| 47 | + var reader = new StreamReader(stream); |
| 48 | + using (var jreader = new JsonTextReader(reader)) |
| 49 | + { |
| 50 | + var serializer = new JsonSerializer(); |
| 51 | + return serializer.Deserialize(jreader, type); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public Task<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default(CancellationToken)) => |
| 56 | + Task.FromResult(Deserialize<T>(stream)); |
| 57 | + |
| 58 | + public Task<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default(CancellationToken)) => |
| 59 | + Task.FromResult(Deserialize(type, stream)); |
| 60 | + |
| 61 | + public void Serialize<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.Indented) |
| 62 | + { |
| 63 | + var writer = new StreamWriter(stream); |
| 64 | + using (var jWriter = new JsonTextWriter(writer)) |
| 65 | + { |
| 66 | + var serializer = new JsonSerializer |
| 67 | + { |
| 68 | + Formatting = formatting == SerializationFormatting.Indented ? Formatting.Indented : Formatting.None |
| 69 | + }; |
| 70 | + serializer.Serialize(jWriter, data); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + public Task SerializeAsync<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.Indented, |
| 76 | + CancellationToken cancellationToken = default(CancellationToken)) |
| 77 | + { |
| 78 | + Serialize<T>(data, stream, formatting); |
| 79 | + return Task.CompletedTask; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments