Skip to content
This repository was archived by the owner on Jun 1, 2024. It is now read-only.

Commit a9359d1

Browse files
tresoldigiorgiomivano
authored andcommitted
added TemplateCustomSettings as Dictionary<string,string> (#311)
1 parent 91748ad commit a9359d1

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticsearchSinkOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ public class ElasticsearchSinkOptions
5858
/// </summary>
5959
public Func<object> GetTemplateContent { get; set; }
6060

61+
/// <summary>
62+
/// When using the <see cref="AutoRegisterTemplate"/> feature, this allows you to override the default template content.
63+
/// If not provided, a default template that is optimized to deal with Serilog events is used.
64+
/// </summary>
65+
public Dictionary<string,string> TemplateCustomSettings { get; set; }
66+
6167
/// <summary>
6268
/// When using the <see cref="AutoRegisterTemplate"/> feature, this allows you to overwrite the template in Elasticsearch if it already exists.
6369
/// Defaults to: false

src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticsearchSinkState.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void RegisterTemplateIfNeeded()
160160
{
161161
var templateExistsResponse = _client.Indices.TemplateExistsForAll<VoidResponse>(_templateName, new IndexTemplateExistsRequestParameters()
162162
{
163-
RequestConfiguration = new RequestConfiguration() { AllowedStatusCodes = new [] {200, 404} }
163+
RequestConfiguration = new RequestConfiguration() { AllowedStatusCodes = new[] { 200, 404 } }
164164
});
165165
if (templateExistsResponse.HttpStatusCode == 200)
166166
{
@@ -173,7 +173,7 @@ public void RegisterTemplateIfNeeded()
173173
var result = _client.Indices.PutTemplateForAll<StringResponse>(_templateName, GetTemplatePostData(),
174174
new PutIndexTemplateRequestParameters
175175
{
176-
IncludeTypeName = IncludeTypeName ? true : (bool?) null
176+
IncludeTypeName = IncludeTypeName ? true : (bool?)null
177177
});
178178

179179
if (!result.Success)
@@ -222,15 +222,15 @@ private object GetTemplateData()
222222
if (_options.GetTemplateContent != null)
223223
return _options.GetTemplateContent();
224224

225-
var settings = new Dictionary<string, string>
226-
{
227-
{"index.refresh_interval", "5s"}
228-
};
225+
var settings = _options.TemplateCustomSettings ?? new Dictionary<string, string>();
226+
227+
if (!settings.ContainsKey("index.refresh_interval"))
228+
settings.Add("index.refresh_interval", "5s");
229229

230-
if (_options.NumberOfShards.HasValue)
230+
if (_options.NumberOfShards.HasValue && !settings.ContainsKey("number_of_shards"))
231231
settings.Add("number_of_shards", _options.NumberOfShards.Value.ToString());
232232

233-
if (_options.NumberOfReplicas.HasValue)
233+
if (_options.NumberOfReplicas.HasValue && !settings.ContainsKey("number_of_replicas"))
234234
settings.Add("number_of_replicas", _options.NumberOfReplicas.Value.ToString());
235235

236236
return ElasticsearchTemplateProvider.GetTemplate(
@@ -245,14 +245,14 @@ private object GetTemplateData()
245245
public void DiscoverClusterVersion()
246246
{
247247
if (!_options.DetectElasticsearchVersion) return;
248-
248+
249249
var response = _client.Cat.Nodes<StringResponse>(new CatNodesRequestParameters()
250-
{
251-
Headers = new [] { "v"}
250+
{
251+
Headers = new[] { "v" }
252252
});
253253
if (!response.Success) return;
254254

255-
_discoveredVersion = response.Body.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries)
255+
_discoveredVersion = response.Body.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
256256
.FirstOrDefault();
257257

258258
if (_discoveredVersion?.StartsWith("7.") ?? false)

0 commit comments

Comments
 (0)