Skip to content

Commit cd2a1c0

Browse files
committed
Polishing ElasticSearch pull request.
1 parent 50750cf commit cd2a1c0

File tree

8 files changed

+111
-110
lines changed

8 files changed

+111
-110
lines changed

src/Serilog.Ui.ElasticSearchProvider/ElasticSearchDbDataProvider.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Nest;
22
using Serilog.Ui.Core;
3-
using Serilog.Ui.MongoDbProvider;
43
using System;
54
using System.Collections.Generic;
65
using System.Linq;
@@ -12,7 +11,6 @@ namespace Serilog.Ui.ElasticSearchProvider
1211
public class ElasticSearchDbDataProvider : IDataProvider
1312
{
1413
private readonly IElasticClient _client;
15-
1614
private readonly ElasticSearchDbOptions _options;
1715

1816
public ElasticSearchDbDataProvider(IElasticClient client, ElasticSearchDbOptions options)
@@ -21,10 +19,11 @@ public ElasticSearchDbDataProvider(IElasticClient client, ElasticSearchDbOptions
2119
_options = options ?? throw new ArgumentNullException(nameof(options));
2220
}
2321

24-
public Task<(IEnumerable<LogModel>, int)> FetchDataAsync(int page,
25-
int count,
26-
string level = null,
27-
string searchCriteria = null)
22+
public Task<(IEnumerable<LogModel>, int)> FetchDataAsync(
23+
int page,
24+
int count,
25+
string level = null,
26+
string searchCriteria = null)
2827
{
2928
return GetLogsAsync(page - 1, count, level, searchCriteria);
3029
}
@@ -38,15 +37,13 @@ public ElasticSearchDbDataProvider(IElasticClient client, ElasticSearchDbOptions
3837
{
3938
var descriptor = new SearchDescriptor<ElasticSearchDbLogModel>()
4039
.Index(_options.IndexName)
40+
.Sort(m => m.Descending(f => f.Timestamp))
4141
.Size(count)
4242
.Skip(page * count);
4343

4444
if (!string.IsNullOrEmpty(level))
45-
descriptor.Query(q => q
46-
.Match(m => m
47-
.Field(f => f.Level)
48-
.Query(level))
49-
);
45+
descriptor.Query(q => q.Match(m => m.Field(f => f.Level).Query(level)));
46+
5047
if (!string.IsNullOrEmpty(searchCriteria))
5148
descriptor.Query(q => q
5249
.Match(m => m
@@ -58,11 +55,10 @@ public ElasticSearchDbDataProvider(IElasticClient client, ElasticSearchDbOptions
5855
.Query(searchCriteria)
5956
)
6057
);
61-
//descriptor = descriptor.Fields(f => f.Field(z => z.Message == searchCriteria));
6258

6359
var result = await _client.SearchAsync<ElasticSearchDbLogModel>(descriptor, cancellationToken);
6460

65-
int.TryParse(result?.Total.ToString(), out int total);
61+
int.TryParse(result?.Total.ToString(), out var total);
6662

6763
return (result?.Documents.Select((x, index) => x.ToLogModel(index)).ToList(), total);
6864
}

src/Serilog.Ui.ElasticSearchProvider/ElasticSearchDbLogModel.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using Newtonsoft.Json;
1+
using Nest;
2+
using Newtonsoft.Json;
23
using Newtonsoft.Json.Linq;
34
using Serilog.Ui.Core;
45
using System;
56
using System.Collections.Generic;
67

7-
namespace Serilog.Ui.MongoDbProvider
8+
namespace Serilog.Ui.ElasticSearchProvider
89
{
9-
public class ElasticSearchDbLogModel
10+
public class ElasticSearchDbLogModel
1011
{
1112
[JsonProperty("level")]
1213
public string Level { get; set; }
@@ -15,6 +16,7 @@ public class ElasticSearchDbLogModel
1516
public string Message { get; set; }
1617

1718
[JsonProperty("@timestamp")]
19+
[Date(Name = "@timestamp")]
1820
public DateTime Timestamp { get; set; }
1921

2022
public JArray Exceptions { get; set; }
@@ -30,13 +32,13 @@ internal LogModel ToLogModel(int index)
3032
Level = Level,
3133
Message = Message,
3234
Timestamp = Timestamp,
33-
Exception = Exceptions?.Count > 0 ? BuildExceptionMessage(Exceptions[0]) : string.Empty,
35+
Exception = Exceptions?.Count > 0 ? BuildExceptionMessage(Exceptions[0]) : null,
3436
Properties = JsonConvert.SerializeObject(Fields),
3537
PropertyType = "json"
3638
};
3739
}
3840

39-
static string BuildExceptionMessage(JToken jObjet)
41+
private static string BuildExceptionMessage(JToken jObjet)
4042
{
4143
return $"Exception: {jObjet.Value<string>("Message")}. StackTrace: {jObjet.Value<string>("StackTraceString")}.";
4244
}
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace Serilog.Ui.ElasticSearchProvider
1+
namespace Serilog.Ui.ElasticSearchProvider
82
{
93
public class ElasticSearchDbOptions
104
{
115
public string IndexName { get; set; }
126
}
13-
}
7+
}
Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
using Elasticsearch.Net;
22
using Microsoft.Extensions.DependencyInjection;
33
using Nest;
4-
using Newtonsoft.Json;
54
using Serilog.Ui.Core;
65
using System;
7-
using System.IO;
8-
using System.Threading;
9-
using System.Threading.Tasks;
106

117
namespace Serilog.Ui.ElasticSearchProvider.Extensions
128
{
139
public static class SerilogUiOptionBuilderExtensions
1410
{
15-
public static void UseElasticSearchDb(this SerilogUiOptionsBuilder optionsBuilder,
16-
Uri endpoint,
17-
string indexName)
11+
public static void UseElasticSearchDb(this SerilogUiOptionsBuilder optionsBuilder, Uri endpoint, string indexName)
1812
{
19-
if (endpoint == null) throw new ArgumentNullException(nameof(endpoint));
20-
if (string.IsNullOrEmpty(indexName)) throw new ArgumentNullException(nameof(indexName));
13+
if (endpoint == null)
14+
throw new ArgumentNullException(nameof(endpoint));
15+
16+
if (string.IsNullOrEmpty(indexName))
17+
throw new ArgumentNullException(nameof(indexName));
2118

2219
var options = new ElasticSearchDbOptions
2320
{
@@ -35,48 +32,4 @@ public static void UseElasticSearchDb(this SerilogUiOptionsBuilder optionsBuilde
3532
builder.Services.AddScoped<IDataProvider, ElasticSearchDbDataProvider>();
3633
}
3734
}
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-
}
35+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Elasticsearch.Net;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.IO;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace Serilog.Ui.ElasticSearchProvider.Extensions
9+
{
10+
internal class VanillaSerializer : IElasticsearchSerializer
11+
{
12+
public T Deserialize<T>(Stream stream) => (T)Deserialize(typeof(T), stream);
13+
14+
public object Deserialize(Type type, Stream stream)
15+
{
16+
var reader = new StreamReader(stream);
17+
18+
using (var jsonTextReader = new JsonTextReader(reader))
19+
{
20+
var serializer = new JsonSerializer();
21+
return serializer.Deserialize(jsonTextReader, type);
22+
}
23+
}
24+
25+
public Task<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default) =>
26+
Task.FromResult(Deserialize<T>(stream));
27+
28+
public Task<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default) =>
29+
Task.FromResult(Deserialize(type, stream));
30+
31+
public void Serialize<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.Indented)
32+
{
33+
var writer = new StreamWriter(stream);
34+
35+
using (var jWriter = new JsonTextWriter(writer))
36+
{
37+
var serializer = new JsonSerializer
38+
{
39+
Formatting = formatting == SerializationFormatting.Indented ? Formatting.Indented : Formatting.None
40+
};
41+
serializer.Serialize(jWriter, data);
42+
}
43+
}
44+
45+
public Task SerializeAsync<T>(
46+
T data,
47+
Stream stream,
48+
SerializationFormatting formatting = SerializationFormatting.Indented,
49+
CancellationToken cancellationToken = default)
50+
{
51+
Serialize(data, stream, formatting);
52+
53+
return Task.CompletedTask;
54+
}
55+
}
56+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
3+
<metadata>
4+
<id>Serilog.UI.ElasticSearchProvider</id>
5+
<version>$version$</version>
6+
<title>Serilog.Ui.ElasticSearchProvider</title>
7+
<authors>Ricardo Demauro - rmauro.dev</authors>
8+
<owners>mo.esmp</owners>
9+
<projectUrl>https://github.com/mo-esmp/serilog-ui</projectUrl>
10+
<license type="expression">MIT</license>
11+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
12+
<description>ElasticSearch data provider for Serilog UI.</description>
13+
<releaseNotes></releaseNotes>
14+
<copyright></copyright>
15+
<tags>serilog serilog-ui serilog.sinks.elasticsearch</tags>
16+
<dependencies>
17+
<group targetFramework=".NETStandard2.0">
18+
<dependency id="Microsoft.Extensions.DependencyInjection.Abstractions" version="3.1.3" exclude="Build,Analyzers" />
19+
<dependency id="Microsoft.Extensions.Logging.Abstractions" version="5.0.0" exclude="Build,Analyzers" />
20+
<dependency id="NEST" version="7.11.1" exclude="Build,Analyzers" />
21+
<dependency id="NEST.JsonNetSerializer" version="7.11.1" exclude="Build,Analyzers" />
22+
<dependency id="Newtonsoft.Json" version="12.0.3" exclude="Build,Analyzers" />
23+
</group>
24+
</dependencies>
25+
</metadata>
26+
<files>
27+
<file src="bin\Release\netstandard2.0\*.dll" target="lib/netstandard2.0" />
28+
</files>
29+
</package>

src/Serilog.Ui.ElasticSearchProvider/Serilog.Ui.MongoDbProvider.nuspec

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/Serilog.Ui.MongoDbProvider/Serilog.Ui.MongoDbProvider.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<id>Serilog.UI.MongoDbProvider</id>
55
<version>$version$</version>
66
<title>Serilog.UI.MongoDbProvider</title>
7-
<authors>Mohsen Esmailpour</authors>
7+
<authors>Christian Haase</authors>
88
<owners>mo.esmp</owners>
99
<projectUrl>https://github.com/mo-esmp/serilog-ui</projectUrl>
1010
<license type="expression">MIT</license>

0 commit comments

Comments
 (0)