|
| 1 | +using BetterStack.Logs.Serilog; |
| 2 | +using Serilog.Configuration; |
| 3 | +using Serilog.Core; |
| 4 | +using Serilog.Events; |
| 5 | +using Serilog.Formatting; |
| 6 | +using Serilog.Sinks.Http.BatchFormatters; |
| 7 | +using Serilog.Sinks.Http.Private.NonDurable; |
| 8 | +using System; |
| 9 | + |
| 10 | +namespace Serilog |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Class containing extension method to <see cref="LoggerConfiguration"/> for Better Stack sink config |
| 14 | + /// </summary> |
| 15 | + public static class LoggerSinkConfigurationExtensions |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Adds a sink that sends log events to Better Stack using preconfigured Serilog.Sinks.Http |
| 19 | + /// The log events are stored in memory in the case that the log server cannot be reached. |
| 20 | + /// </summary> |
| 21 | + /// <param name="sinkConfiguration">The logger configuration.</param> |
| 22 | + /// <param name="sourceToken"> |
| 23 | + /// Your source token (taken from https://logs.betterstack.com/dashboard -> Sources -> Edit) |
| 24 | + /// </param> |
| 25 | + /// <param name="betterStackEndpoint"> |
| 26 | + /// The URI of the Better Stack endpoint your logs are sent to. Default value is https://in.logs.betterstack.com. |
| 27 | + /// </param> |
| 28 | + /// <param name="queueLimitBytes"> |
| 29 | + /// The maximum size of events stored in memory, waiting to be sent. Default value is null (no limit). |
| 30 | + /// </param> |
| 31 | + /// <param name="batchSize"> |
| 32 | + /// The maximum number of log events sent as a single batch. Default value is 1000. |
| 33 | + /// </param> |
| 34 | + /// <param name="batchInterval"> |
| 35 | + /// The maximum time before sending logs to Better Stack. Default value is 1 second. |
| 36 | + /// </param> |
| 37 | + /// <param name="restrictedToMinimumLevel"> |
| 38 | + /// The minimum level for events passed through the sink. Ignored if <paramref name="levelSwitch"/> specified. |
| 39 | + /// Default value is <see cref="LevelAlias.Minimum"/>. |
| 40 | + /// </param> |
| 41 | + /// <param name="levelSwitch"> |
| 42 | + /// A switch allowing the pass-through level to be changed at runtime. |
| 43 | + /// </param> |
| 44 | + public static LoggerConfiguration BetterStack( |
| 45 | + this LoggerSinkConfiguration sinkConfiguration, |
| 46 | + string sourceToken, |
| 47 | + string betterStackEndpoint = "https://in.logs.betterstack.com", |
| 48 | + long? queueLimitBytes = null, |
| 49 | + int? batchSize = null, |
| 50 | + TimeSpan? batchInterval = null, |
| 51 | + LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, |
| 52 | + LoggingLevelSwitch? levelSwitch = null) |
| 53 | + { |
| 54 | + if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration)); |
| 55 | + |
| 56 | + batchSize ??= 1000; |
| 57 | + batchInterval ??= TimeSpan.FromSeconds(1); |
| 58 | + |
| 59 | + var sink = new HttpSink( |
| 60 | + requestUri: betterStackEndpoint, |
| 61 | + queueLimitBytes: queueLimitBytes, |
| 62 | + logEventLimitBytes: null, |
| 63 | + logEventsInBatchLimit: batchSize, |
| 64 | + batchSizeLimitBytes: null, |
| 65 | + period: batchInterval.Value, |
| 66 | + textFormatter: new BetterStackTextFormatter(), |
| 67 | + batchFormatter: new ArrayBatchFormatter(), |
| 68 | + httpClient: new BetterStackHttpClient(sourceToken)); |
| 69 | + |
| 70 | + return sinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments