Skip to content

Commit 3f0c012

Browse files
AmirSassonmivano
authored andcommitted
Add Support for specifying Http Headers to be added to the connection in the config file (serilog-contrib#90)
1 parent bb0fb43 commit 3f0c012

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ This example shows the options that are currently available when using the appSe
4545
<add key="serilog:write-to:Elasticsearch.minimumLogEventLevel" value="Warning"/>
4646
<add key="serilog:write-to:Elasticsearch.bufferBaseFilename" value="C:\Temp\SerilogElasticBuffer"/>
4747
<add key="serilog:write-to:Elasticsearch.bufferFileSizeLimitBytes" value="5242880"/>
48-
<add key="serilog:write-to:Elasticsearch.bufferLogShippingInterval" value="5000"/>
48+
<add key="serilog:write-to:Elasticsearch.bufferLogShippingInterval" value="5000"/>
49+
<add key="serilog:write-to:Elasticsearch.connectionGlobalHeaders" value="Authorization=Bearer SOME-TOKEN;OtherHeader=OTHER-HEADER-VALUE" />
4950
</appSettings>
5051
```
5152

@@ -118,7 +119,8 @@ In your `appsettings.json` file, under the `Serilog` node, :
118119
"minimumLogEventLevel": "Warning",
119120
"bufferBaseFilename": "C:/Temp/LogDigipolis/docker-elk-serilog-web-buffer",
120121
"bufferFileSizeLimitBytes": 5242880,
121-
"bufferLogShippingInterval": 5000
122+
"bufferLogShippingInterval": 5000,
123+
"connectionGlobalHeaders" :"Authorization=Bearer SOME-TOKEN;OtherHeader=OTHER-HEADER-VALUE"
122124
}
123125
}]
124126
}

src/Serilog.Sinks.Elasticsearch/LoggerConfigurationElasticSearchExtensions.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Serilog.Core;
2121
using Serilog.Events;
2222
using Serilog.Sinks.Elasticsearch;
23+
using System.Collections.Specialized;
2324

2425
namespace Serilog
2526
{
@@ -76,6 +77,7 @@ public static LoggerConfiguration Elasticsearch(
7677
/// <param name="bufferBaseFilename"><see cref="ElasticsearchSinkOptions.BufferBaseFilename"/></param>
7778
/// <param name="bufferFileSizeLimitBytes"><see cref="ElasticsearchSinkOptions.BufferFileSizeLimitBytes"/></param>
7879
/// <param name="bufferLogShippingInterval"><see cref="ElasticsearchSinkOptions.BufferLogShippingInterval"/></param>
80+
/// <param name="connectionGlobalHeaders">A comma or semi column separated list of key value pairs of headers to be added to each elastic http request</param>
7981
/// <returns>LoggerConfiguration object</returns>
8082
/// <exception cref="ArgumentNullException"><paramref name="nodeUris"/> is <see langword="null" />.</exception>
8183
public static LoggerConfiguration Elasticsearch(
@@ -90,7 +92,8 @@ public static LoggerConfiguration Elasticsearch(
9092
LogEventLevel minimumLogEventLevel = LogEventLevel.Information,
9193
string bufferBaseFilename = null,
9294
long? bufferFileSizeLimitBytes = null,
93-
long bufferLogShippingInterval = 5000)
95+
long bufferLogShippingInterval = 5000,
96+
string connectionGlobalHeaders = null)
9497
{
9598
if (string.IsNullOrEmpty(nodeUris))
9699
throw new ArgumentNullException("nodeUris", "No Elasticsearch node(s) specified.");
@@ -135,6 +138,22 @@ public static LoggerConfiguration Elasticsearch(
135138

136139
options.BufferLogShippingInterval = TimeSpan.FromMilliseconds(bufferLogShippingInterval);
137140

141+
if (!string.IsNullOrWhiteSpace(connectionGlobalHeaders))
142+
{
143+
NameValueCollection headers = new NameValueCollection();
144+
connectionGlobalHeaders
145+
.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)
146+
.ToList()
147+
.ForEach(headerValueStr =>
148+
{
149+
var headerValue = headerValueStr.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
150+
headers.Add(headerValue[0], headerValue[1]);
151+
});
152+
153+
options.ModifyConnectionSettings = (c) => c.GlobalHeaders(headers);
154+
}
155+
156+
138157
return Elasticsearch(loggerSinkConfiguration, options);
139158
}
140159
}

0 commit comments

Comments
 (0)