public static LoggerConfiguration TitanLoki(this LoggerSinkConfiguration sinkConfiguration, LokiCredentials credentials,
                                                    ILogLabelProvider logLabelProvider, IHttpClient httpClient, LogEventLevel logLevelRestriction,
                                                    int batchPostingLimit, TimeSpan period, int queueLimit, bool dynamicLevel)
        {
            BatchFormatter formatter = logLabelProvider != null
                                           ? new BatchFormatter(logLabelProvider.GetLabels())
                                           : new BatchFormatter();

            IHttpClient client = httpClient ?? new LokiHttpClient();

            if (client is LokiHttpClient c)
            {
                c.SetAuthCredentials(credentials);
            }

            if (dynamicLevel)
            {
                return(sinkConfiguration.Http(LokiRouteBuilder.BuildPostUri(credentials.Url), batchFormatter: formatter, httpClient: client,
                                              batchPostingLimit: batchPostingLimit, period: period,
                                              queueLimit: queueLimit));
            }

            return(sinkConfiguration.Http(LokiRouteBuilder.BuildPostUri(credentials.Url), batchFormatter: formatter, httpClient: client,
                                          restrictedToMinimumLevel: logLevelRestriction, batchPostingLimit: batchPostingLimit, period: period,
                                          queueLimit: queueLimit));
        }
        private static LoggerConfiguration LokiHttpImpl(this LoggerSinkConfiguration sinkConfiguration, LokiCredentials credentials, ILogLabelProvider logLabelProvider, IHttpClient httpClient)
        {
            var formatter = logLabelProvider != null ? new LokiBatchFormatter(logLabelProvider.GetLabels()) : new LokiBatchFormatter();

            var client = httpClient ?? new LokiHttpClient();

            if (client is LokiHttpClient c)
            {
                c.SetAuthCredentials(credentials);
            }

            return(sinkConfiguration.Http(LokiRouteBuilder.BuildPostUri(credentials.Url), batchFormatter: formatter, httpClient: client));
        }
Exemple #3
0
        private string GenerateLabels(LogEvent le)
        {
            var list = new List <string> {
                $@"level={le.Level.ToString().ToLower().NormalizeLokiLabelValue()}"
            };

            if (_globalLabelsProvider != null)
            {
                list.AddRange(_globalLabelsProvider.GetLabels()
                              .Select(label => $@"{label.Key}={label.Value.NormalizeLokiLabelValue()}"));
            }

            if (le.Exception != null)
            {
                list.Add($"ExceptionType={le.Exception.GetType().Name.NormalizeLokiLabelValue()}");
                if (_stackTraceAsLabel)
                {
                    list.Add(
                        $"StackTrace={System.Web.HttpUtility.HtmlEncode(le.Exception.StackTrace).NormalizeLokiLabelValue()}");
                }

                if (le.Exception.Data?.Count != 0)
                {
                    foreach (var key in le.Exception.Data.Keys)
                    {
                        var value = le.Exception.Data[key];
                        if (value != null)
                        {
                            list.Add($"Exception_Data_{key}={value.ToString().NormalizeLokiLabelValue()}");
                        }
                    }
                }
            }

            list.AddRange(le.Properties.Select(x => $@"{x.Key}={x.Value.ToString().NormalizeLokiLabelValue()}"));
            return($"{{{string.Join(",", list)}}}");
        }