@@ -17,44 +17,44 @@ namespace AM.Extensions.Logging.ElasticSearch
17
17
public class ElasticsearchLoggerProvider : ILoggerProvider
18
18
{
19
19
#region fields
20
+ private readonly IOptionsMonitor < ElasticsearchLoggerOptions > _optionsMonitor ;
20
21
private IElasticLowLevelClient _client ;
21
- private readonly Uri _endpoint ;
22
- private readonly string _indexPrefix ;
23
22
private readonly BlockingCollection < JObject > _queueToBePosted = new BlockingCollection < JObject > ( ) ;
24
-
25
23
private const string DocumentType = "doc" ;
26
24
private Action < JObject > _scribeProcessor ;
27
-
28
25
#endregion
29
26
30
-
31
-
32
27
/// <summary>
33
28
/// prefix for the Index for traces
34
29
/// </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" ) ;
37
31
38
- public ElasticsearchLoggerProvider ( IOptions < ElasticsearchLoggerOptions > options ) : this ( options . Value )
39
- { }
40
32
41
- public ElasticsearchLoggerProvider ( ElasticsearchLoggerOptions options )
33
+ public ElasticsearchLoggerProvider ( IOptionsMonitor < ElasticsearchLoggerOptions > optionsMonitor )
42
34
{
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 ) ;
45
42
46
- //build the client
47
- //build the batcher
48
43
Initialize ( ) ;
44
+ }
49
45
46
+ private void UpdateClientWithNewOptions ( ElasticsearchLoggerOptions newOptions )
47
+ {
48
+ var newClient = CreateNewElasticLowLevelClient ( newOptions . ElasticsearchEndpoint ) ;
49
+
50
+ _client = newClient ;
50
51
}
51
52
52
53
public ILogger CreateLogger ( string categoryName )
53
54
{
54
55
return new ElasticsearchLogger ( categoryName , _scribeProcessor ) ;
55
56
}
56
57
57
-
58
58
public IElasticLowLevelClient Client
59
59
{
60
60
get
@@ -65,22 +65,25 @@ public IElasticLowLevelClient Client
65
65
}
66
66
else
67
67
{
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 ) ;
77
69
78
- this . _client = new ElasticLowLevelClient ( cc ) ;
79
70
return this . _client ;
80
71
}
81
72
}
82
73
}
83
74
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
+
84
87
private void Initialize ( )
85
88
{
86
89
//setup a flag in config to chose
@@ -141,7 +144,7 @@ await Client.BulkPutAsync<VoidResponse>(Index, DocumentType,
141
144
PostData . MultiJson ( bbo . ToArray ( ) ) ,
142
145
new BulkRequestParameters { Refresh = Refresh . False } ) ;
143
146
}
144
- catch ( Exception ex )
147
+ catch ( Exception )
145
148
{
146
149
//eat the exception, we cant really do much with it anyways
147
150
//Debug.WriteLine(ex.Message);
@@ -153,21 +156,6 @@ private void WriteToQueueForProcessing(JObject jo)
153
156
this . _queueToBePosted . Add ( jo ) ;
154
157
}
155
158
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
159
#region IDisposable Support
172
160
private bool disposedValue = false ; // To detect redundant calls
173
161
0 commit comments