forked from josephwoodward/Serilog-Sinks-Loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LokiSinkExtensions.cs
52 lines (47 loc) · 2.2 KB
/
LokiSinkExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using Serilog.Configuration;
using Serilog.Formatting.Display;
using Serilog.Sinks.Http;
using Serilog.Sinks.Loki.Labels;
namespace Serilog.Sinks.Loki
{
public static class LokiSinkExtensions
{
public static LoggerConfiguration LokiHttp(this LoggerSinkConfiguration sinkConfiguration, Func<LokiSinkConfiguration> configFactory)
=> LokiHttpImpl(sinkConfiguration, configFactory());
private static LoggerConfiguration LokiHttpImpl(this LoggerSinkConfiguration serilogConfig, LokiSinkConfiguration lokiConfig)
{
var credentials = string.IsNullOrWhiteSpace(lokiConfig.LokiUsername)
? (LokiCredentials)new NoAuthCredentials(lokiConfig.LokiUrl)
: new BasicAuthCredentials(lokiConfig.LokiUrl, lokiConfig.LokiUsername, lokiConfig.LokiPassword);
return LokiHttpImpl(serilogConfig, credentials, lokiConfig.LogLabelProvider, lokiConfig.HttpClient,
lokiConfig.OutputTemplate, lokiConfig.FormatProvider, lokiConfig.BatchPostingLimit,
lokiConfig.QueueLimit, lokiConfig.Period);
}
private static LoggerConfiguration LokiHttpImpl(
this LoggerSinkConfiguration sinkConfiguration,
LokiCredentials credentials,
ILogLabelProvider logLabelProvider,
IHttpClient httpClient,
string outputTemplate,
IFormatProvider formatProvider,
int batchPostingLimit,
int? queueLimit,
TimeSpan? period)
{
var formatter = new LokiBatchFormatter(logLabelProvider ?? new DefaultLogLabelProvider());
var client = httpClient ?? new DefaultLokiHttpClient();
if (client is LokiHttpClient c)
{
c.SetAuthCredentials(credentials);
}
return sinkConfiguration.Http(LokiRouteBuilder.BuildPostUri(credentials.Url),
batchFormatter: formatter,
textFormatter: new MessageTemplateTextFormatter(outputTemplate, formatProvider),
httpClient: client,
batchPostingLimit: batchPostingLimit,
queueLimit: queueLimit,
period: period);
}
}
}