diff --git a/BetterStack.Logs.NLog/BetterStackLogsTarget.cs b/BetterStack.Logs.NLog/BetterStackLogsTarget.cs index 5bab807..d979907 100644 --- a/BetterStack.Logs.NLog/BetterStackLogsTarget.cs +++ b/BetterStack.Logs.NLog/BetterStackLogsTarget.cs @@ -113,6 +113,16 @@ protected override void InitializeTarget() if (!Uri.TryCreate(endpointUrl, UriKind.Absolute, out _)) { throw new NLogConfigurationException($"BetterStack.Logs: endpoint is \"{endpoint}\". Set it to the ingesting host of your Better Stack source."); } + // 0 would leave the drain spinning on a core: without taking any logs, or without waiting between flushes + if (MaxBatchSize < 1) { + throw new NLogConfigurationException($"BetterStack.Logs: maxBatchSize is {MaxBatchSize}. Set it to 1 or more."); + } + if (FlushPeriodMilliseconds < 1) { + throw new NLogConfigurationException($"BetterStack.Logs: flushPeriodMilliseconds is {FlushPeriodMilliseconds}. Set it to 1 or more."); + } + if (Retries < 0) { + throw new NLogConfigurationException($"BetterStack.Logs: retries is {Retries}. Set it to 0 or more."); + } var client = new Client( sourceToken, diff --git a/tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs b/tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs index 2724996..cbea138 100644 --- a/tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs +++ b/tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs @@ -480,5 +480,48 @@ public void ConfiguresFromXml() Assert.Equal("Tracing the code!", (string)log["message"]); Assert.Equal("Trace", (string)log["level"]); } + + [Fact] + public void ReportsMaxBatchSizeBelowOneAsConfigurationError() + { + logFactory.ThrowConfigExceptions = true; + var target = Target(); + target.MaxBatchSize = 0; + + var exception = Assert.Throws(() => LoggerFor(target)); + Assert.Equal("BetterStack.Logs: maxBatchSize is 0. Set it to 1 or more.", exception.Message); + } + + [Fact] + public void ReportsFlushPeriodBelowOneMillisecondAsConfigurationError() + { + logFactory.ThrowConfigExceptions = true; + var target = Target(); + target.FlushPeriodMilliseconds = 0; + + var exception = Assert.Throws(() => LoggerFor(target)); + Assert.Equal("BetterStack.Logs: flushPeriodMilliseconds is 0. Set it to 1 or more.", exception.Message); + } + + [Fact] + public void ReportsNegativeRetriesAsConfigurationError() + { + logFactory.ThrowConfigExceptions = true; + var target = Target(); + target.Retries = -1; + + var exception = Assert.Throws(() => LoggerFor(target)); + Assert.Equal("BetterStack.Logs: retries is -1. Set it to 0 or more.", exception.Message); + } + + [Fact] + public void AcceptsZeroRetries() + { + logFactory.ThrowConfigExceptions = true; + var target = Target(); + target.Retries = 0; + + Assert.Null(Record.Exception(() => LoggerFor(target))); + } } }