@@ -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