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 void PostLoggingEvent(LoggingEvent[] loggingEvents)
        {
            var formatter  = new LokiBatchFormatter(labels);
            var httpClient = new LokiHttpClient(TrustSelfCignedCerts);

            if (httpClient is LokiHttpClient c)
            {
                LokiCredentials credentials;

                if (!string.IsNullOrEmpty(BasicAuthUserName) && !string.IsNullOrEmpty(BasicAuthPassword))
                {
                    credentials = new BasicAuthCredentials(ServiceUrl, BasicAuthUserName, BasicAuthPassword);
                }
                else
                {
                    credentials = new NoAuthCredentials(ServiceUrl);
                }

                c.SetAuthCredentials(credentials);
            }

            using (MemoryStream ms = new MemoryStream())
                using (var sc = new StreamWriter(ms))
                {
                    formatter.Format(loggingEvents, sc);
                    sc.Flush();
                    ms.Position = 0;
                    var content    = new StreamContent(ms);
                    var contentStr = content.ReadAsStringAsync().Result; // TO VERIFY
                    httpClient.PostAsync(LokiRouteBuilder.BuildPostUri(ServiceUrl), content);
                }
        }
示例#3
0
        public void RequestUriIsCorrect(string address)
        {
            // Arrange
            var credentials = new LokiCredentials(address);
            var log         = new LoggerConfiguration()
                              .MinimumLevel.Information()
                              .WriteTo.LokiHttp(credentials, httpClient: _client)
                              .CreateLogger();

            // Act
            log.Error("Something's wrong");
            log.Dispose();

            // Assert
            _client.RequestUri.ShouldBe(LokiRouteBuilder.BuildPostUri(credentials.Url));
        }
示例#4
0
        public void RequestUriIsCorrect(string address)
        {
            // Arrange
            var log = new LoggerConfiguration()
                      .MinimumLevel.Information()
                      .WriteTo.LokiHttp(() => new LokiSinkConfiguration
            {
                LokiUrl    = address,
                HttpClient = _client
            })
                      .CreateLogger();

            // Act
            log.Error("Something's wrong");
            log.Dispose();

            // Assert
            _client.RequestUri.ShouldBe(LokiRouteBuilder.BuildPostUri(address));
        }