Skip to content

Commit ea88338

Browse files
Alex McCoolBrian Stauff
andauthored
from @bstauff - Bring options monitor into LoggerProvider (#37)
* I guess we need this afterall? * removeing the ILoggerFactory interactions to properly introduce the options * final cleanups Co-authored-by: Brian Stauff <[email protected]>
1 parent c1b5b83 commit ea88338

File tree

7 files changed

+37
-65
lines changed

7 files changed

+37
-65
lines changed

src/ElasticLogger.Test/ESFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public ESFixture(IMessageSink messageSink)
2020

2121
_elasticsearch = new ElasticsearchInside.Elasticsearch(c => c.SetElasticsearchStartTimeout(60)
2222
.EnableLogging()
23-
.LogTo(s => _messageSink.OnMessage(new Xunit.Sdk.DiagnosticMessage(s ?? string.Empty))));
23+
.LogTo(s => _messageSink.OnMessage(new Xunit.Sdk.DiagnosticMessage(s ?? string.Empty)))
24+
);
2425

2526
_elasticsearch.ReadySync();
2627
}

src/ElasticLogger.Test/ElasticLogger.Test.csproj

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@
1010
<PackageReference Include="elasticsearch-inside" Version="6.6.1" />
1111
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
1212
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.3" />
13-
<!-- <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
14-
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
15-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
16-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
17-
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
18-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.*" /> -->
19-
2013
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0" />
2114
<PackageReference Include="NEST" Version="6.0.2" />
2215
<PackageReference Include="xunit" Version="2.4.1" />

src/ElasticLogger.Test/ElasticsearchLoggerFilterTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public async Task No_categories_for_es_should_use_default()
8686

8787

8888
[Fact]
89-
public async Task configured_categories_dont_log_for_config()
89+
public async Task Configured_categories_dont_log_for_config()
9090
{
9191
var source = "Billy.bob";
9292

@@ -277,7 +277,6 @@ private async Task LevelTesterPositive(string source, LogLevel logLevel)
277277
var prov = services.BuildServiceProvider();
278278

279279
var loggerFactory = prov.GetService<ILoggerFactory>();
280-
281280
var logger = loggerFactory.CreateLogger(source);
282281

283282
var circularRefObj = new Circle();

src/ElasticLogger.Test/MiscTests.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@ public MiscTests(ITestOutputHelper outputHelper)
1616
_output = outputHelper;
1717
}
1818

19-
20-
2119
[Fact]
2220
public async Task TryingToBreakThisThing()
2321
{
24-
JsonSerializerSettings settings = new JsonSerializerSettings { };
25-
2622
Circle sillyCircle = new Circle{me = null};
2723
sillyCircle.me = sillyCircle;
2824

@@ -34,7 +30,7 @@ public async Task TryingToBreakThisThing()
3430

3531
var reader = new StreamReader(stream);
3632

37-
var huh = await reader.ReadToEndAsync();
33+
await reader.ReadToEndAsync();
3834
}
3935

4036
[Fact(Skip ="issues with hosting")]

src/ElasticLogger.Test/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class Startup
1414
{
1515
public void ConfigureServices(IServiceCollection services)
1616
{
17-
//services.AddMvc();
17+
services.AddMvc();
1818
}
1919

2020
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
21-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
21+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
2222
{
2323
app.Use(async (context, next) =>
2424
{

src/ElasticSearch.Extensions.Logging/ElasticSearchLogger.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ protected void WriteTrace(
107107
{
108108
updatedMessage = ((Exception)data).Message;
109109

110-
111110
payload = JObject.FromObject(data, serializerIgnoreReferenceLoop);
112111
}
113112
else if (data is XPathNavigator)
@@ -232,15 +231,11 @@ private void InternalWrite(
232231

233232
_scribeProcessor(jo);
234233
}
235-
catch (Exception ex)
234+
catch (Exception)
236235
{
237236
//this is of questionable value
238237
//Debug.WriteLine(ex.Message);
239238
}
240239
}
241-
242-
243-
244-
245240
}
246241
}

src/ElasticSearch.Extensions.Logging/ElasticSearchLoggerProvider.cs

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,44 @@ namespace AM.Extensions.Logging.ElasticSearch
1717
public class ElasticsearchLoggerProvider : ILoggerProvider
1818
{
1919
#region fields
20+
private readonly IOptionsMonitor<ElasticsearchLoggerOptions> _optionsMonitor;
2021
private IElasticLowLevelClient _client;
21-
private readonly Uri _endpoint;
22-
private readonly string _indexPrefix;
2322
private readonly BlockingCollection<JObject> _queueToBePosted = new BlockingCollection<JObject>();
24-
2523
private const string DocumentType = "doc";
2624
private Action<JObject> _scribeProcessor;
27-
2825
#endregion
2926

30-
31-
3227
/// <summary>
3328
/// prefix for the Index for traces
3429
/// </summary>
35-
private string Index => this._indexPrefix.ToLower() + "-" + DateTime.UtcNow.ToString("yyyy-MM-dd-HH");
36-
30+
private string Index => _optionsMonitor.CurrentValue.IndexName.ToLower() + "-" + DateTime.UtcNow.ToString("yyyy-MM-dd-HH");
3731

38-
public ElasticsearchLoggerProvider(IOptions<ElasticsearchLoggerOptions> options) : this(options.Value)
39-
{ }
4032

41-
public ElasticsearchLoggerProvider(ElasticsearchLoggerOptions options)
33+
public ElasticsearchLoggerProvider(IOptionsMonitor<ElasticsearchLoggerOptions> optionsMonitor)
4234
{
43-
_endpoint = options.ElasticsearchEndpoint;
44-
_indexPrefix = options.IndexName;
35+
if(optionsMonitor == null)
36+
{
37+
throw new ArgumentNullException(nameof(optionsMonitor));
38+
}
39+
_optionsMonitor = optionsMonitor;
40+
41+
_optionsMonitor.OnChange(UpdateClientWithNewOptions);
4542

46-
//build the client
47-
//build the batcher
4843
Initialize();
44+
}
4945

46+
private void UpdateClientWithNewOptions(ElasticsearchLoggerOptions newOptions)
47+
{
48+
var newClient = CreateNewElasticLowLevelClient(newOptions.ElasticsearchEndpoint);
49+
50+
_client = newClient;
5051
}
5152

5253
public ILogger CreateLogger(string categoryName)
5354
{
5455
return new ElasticsearchLogger(categoryName, _scribeProcessor);
5556
}
5657

57-
5858
public IElasticLowLevelClient Client
5959
{
6060
get
@@ -65,22 +65,25 @@ public IElasticLowLevelClient Client
6565
}
6666
else
6767
{
68-
var singleNode = new SingleNodeConnectionPool(_endpoint);
69-
70-
var cc = new ConnectionConfiguration(singleNode, new ElasticsearchJsonNetSerializer())
71-
.EnableHttpPipelining()
72-
.EnableHttpCompression()
73-
.ThrowExceptions();
74-
75-
//the 1.x serializer we needed to use, as the default SimpleJson didnt work right
76-
//Elasticsearch.Net.JsonNet.ElasticsearchJsonNetSerializer()
68+
this._client = CreateNewElasticLowLevelClient(_optionsMonitor.CurrentValue.ElasticsearchEndpoint);
7769

78-
this._client = new ElasticLowLevelClient(cc);
7970
return this._client;
8071
}
8172
}
8273
}
8374

75+
private ElasticLowLevelClient CreateNewElasticLowLevelClient(Uri elasticSearchEndpoint)
76+
{
77+
var singleNode = new SingleNodeConnectionPool(_optionsMonitor.CurrentValue.ElasticsearchEndpoint);
78+
79+
var cc = new ConnectionConfiguration(singleNode, new ElasticsearchJsonNetSerializer())
80+
.EnableHttpPipelining()
81+
.EnableHttpCompression()
82+
.ThrowExceptions();
83+
84+
return new ElasticLowLevelClient(cc);
85+
}
86+
8487
private void Initialize()
8588
{
8689
//setup a flag in config to chose
@@ -141,7 +144,7 @@ await Client.BulkPutAsync<VoidResponse>(Index, DocumentType,
141144
PostData.MultiJson(bbo.ToArray()),
142145
new BulkRequestParameters { Refresh = Refresh.False });
143146
}
144-
catch (Exception ex)
147+
catch (Exception)
145148
{
146149
//eat the exception, we cant really do much with it anyways
147150
//Debug.WriteLine(ex.Message);
@@ -153,21 +156,6 @@ private void WriteToQueueForProcessing(JObject jo)
153156
this._queueToBePosted.Add(jo);
154157
}
155158

156-
157-
158-
159-
160-
161-
162-
163-
164-
165-
166-
167-
168-
169-
170-
171159
#region IDisposable Support
172160
private bool disposedValue = false; // To detect redundant calls
173161

0 commit comments

Comments
 (0)