Skip to content
10 changes: 10 additions & 0 deletions BetterStack.Logs.NLog/BetterStackLogsTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 43 additions & 0 deletions tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@
LoggerFor(target).Info("Hello");

var connection = server.AcceptTcpClientAsync();
Assert.True(connection.Wait(TimeSpan.FromSeconds(30)), "No connection arrived within 30 seconds.");

Check warning on line 137 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net48 with NLog 5.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 137 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net8.0 with NLog 4.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 137 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net10.0 with NLog 4.7.11

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 137 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net10.0 with NLog 4.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 137 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net8.0 with NLog 5.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 137 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net48 with NLog 4.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 137 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net8.0 with NLog 4.7.11

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 137 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net48 with NLog 6.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 137 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net48 with NLog 4.7.11

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 137 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net10.0 with NLog 5.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 137 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net8.0 with NLog 6.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 137 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net10.0 with NLog 6.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)
using (var client = connection.Result) {

Check warning on line 138 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net48 with NLog 5.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 138 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net8.0 with NLog 4.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 138 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net10.0 with NLog 4.7.11

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 138 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net10.0 with NLog 4.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 138 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net8.0 with NLog 5.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 138 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net48 with NLog 4.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 138 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net8.0 with NLog 4.7.11

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 138 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net48 with NLog 6.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 138 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net48 with NLog 4.7.11

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 138 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net10.0 with NLog 5.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 138 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net8.0 with NLog 6.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 138 in tests/BetterStack.Logs.NLog.Tests/BetterStackLogsTargetTests.cs

View workflow job for this annotation

GitHub Actions / Test on net10.0 with NLog 6.*

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)
client.ReceiveTimeout = 30000;
// A TLS handshake record starts with 0x16, a plain http request with "POST"
Assert.Equal(0x16, client.GetStream().ReadByte());
Expand Down Expand Up @@ -480,5 +480,48 @@
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<NLogConfigurationException>(() => 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<NLogConfigurationException>(() => 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<NLogConfigurationException>(() => 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)));
}
}
}
Loading