diff --git a/src/Exceptionless/Extensions/ExceptionlessConfigurationExtensions.cs b/src/Exceptionless/Extensions/ExceptionlessConfigurationExtensions.cs index 6050d569..9d065ee8 100644 --- a/src/Exceptionless/Extensions/ExceptionlessConfigurationExtensions.cs +++ b/src/Exceptionless/Extensions/ExceptionlessConfigurationExtensions.cs @@ -497,7 +497,7 @@ public static void ReadFromEnvironmentalVariables(this ExceptionlessConfiguratio if (String.IsNullOrEmpty(processQueueOnCompletedRequestValue)) { // check for AWS lambda environment - if (!String.IsNullOrEmpty(GetEnvironmentalVariable("AWS_LAMBDA_FUNCTION_NAME "))) + if (!String.IsNullOrEmpty(GetEnvironmentalVariable("AWS_LAMBDA_FUNCTION_NAME"))) processQueueOnCompletedRequestValue = Boolean.TrueString; // check for azure functions environment diff --git a/src/Exceptionless/Plugins/Default/100_HeartbeatPlugin.cs b/src/Exceptionless/Plugins/Default/100_HeartbeatPlugin.cs index 65a56f8d..b93a4b3c 100644 --- a/src/Exceptionless/Plugins/Default/100_HeartbeatPlugin.cs +++ b/src/Exceptionless/Plugins/Default/100_HeartbeatPlugin.cs @@ -14,7 +14,7 @@ public class HeartbeatPlugin : IEventPlugin, IDisposable { /// /// The interval at which heartbeats are sent after the last sent event. The default is 1 minutes. public HeartbeatPlugin(TimeSpan? interval = null) { - _interval = interval.HasValue && interval.Value.Seconds >= 30 ? interval.Value : TimeSpan.FromMinutes(1); + _interval = interval.HasValue && interval.Value >= TimeSpan.FromSeconds(30) ? interval.Value : TimeSpan.FromMinutes(1); } public void Run(EventPluginContext context) { diff --git a/src/Exceptionless/Queue/DefaultEventQueue.cs b/src/Exceptionless/Queue/DefaultEventQueue.cs index fa246c55..e9d59c1b 100644 --- a/src/Exceptionless/Queue/DefaultEventQueue.cs +++ b/src/Exceptionless/Queue/DefaultEventQueue.cs @@ -60,6 +60,7 @@ public async Task ProcessAsync() { var batch = _storage.GetEventBatch(queueName, batchSize, maxCreatedDate); while (batch.Any()) { bool deleteBatch = true; + bool retryWithSmallerBatch = false; try { var events = batch.Select(b => b.Item2).ToList(); @@ -88,6 +89,7 @@ public async Task ProcessAsync() { _log.Error(typeof(DefaultEventQueue), "Event submission discarded for being too large. The event will be retried with a smaller batch size."); batchSize = Math.Max(1, (int)Math.Round(batchSize / 1.5d, 0)); deleteBatch = false; + retryWithSmallerBatch = true; } else { _log.Error(typeof(DefaultEventQueue), "Event submission discarded for being too large. The event will not be submitted."); } @@ -115,7 +117,7 @@ public async Task ProcessAsync() { else _storage.ReleaseBatch(batch); - if (!deleteBatch || IsQueueProcessingSuspended) + if ((!deleteBatch && !retryWithSmallerBatch) || IsQueueProcessingSuspended) break; batch = _storage.GetEventBatch(queueName, batchSize, maxCreatedDate); diff --git a/src/Platforms/Exceptionless.AspNetCore/readme.txt b/src/Platforms/Exceptionless.AspNetCore/readme.txt index 75af1576..2ada9987 100644 --- a/src/Platforms/Exceptionless.AspNetCore/readme.txt +++ b/src/Platforms/Exceptionless.AspNetCore/readme.txt @@ -5,13 +5,13 @@ Exceptionless provides real-time error reporting for your apps. It organizes the gathered information into simple actionable data that will help your app become exceptionless! -Learn more at http://exceptionless.io. +Learn more at https://exceptionless.com/. ------------------------------------- How to get an api key ------------------------------------- The Exceptionless client requires an api key to use the Exceptionless service. -You can get your Exceptionless api key by logging into http://exceptionless.io +You can get your Exceptionless api key by logging into https://be.exceptionless.io/ and viewing your project configuration page. ------------------------------------- @@ -64,4 +64,4 @@ services.AddHttpContextAccessor() ------------------------------------- Documentation and Support ------------------------------------- -Please visit http://exceptionless.io for documentation and support. +Please visit https://exceptionless.com/docs/ for documentation and support. diff --git a/src/Platforms/Exceptionless.Extensions.Hosting/readme.txt b/src/Platforms/Exceptionless.Extensions.Hosting/readme.txt index 93c4b150..cfd47616 100644 --- a/src/Platforms/Exceptionless.Extensions.Hosting/readme.txt +++ b/src/Platforms/Exceptionless.Extensions.Hosting/readme.txt @@ -5,13 +5,13 @@ Exceptionless provides real-time error reporting for your apps. It organizes the gathered information into simple actionable data that will help your app become exceptionless! -Learn more at http://exceptionless.io. +Learn more at https://exceptionless.com/. ------------------------------------- How to get an api key ------------------------------------- The Exceptionless client requires an api key to use the Exceptionless service. -You can get your Exceptionless api key by logging into http://exceptionless.io +You can get your Exceptionless api key by logging into https://be.exceptionless.io/ and viewing your project configuration page. ------------------------------------- @@ -42,4 +42,4 @@ for examples on sending events to Exceptionless. ------------------------------------- Documentation and Support ------------------------------------- -Please visit http://exceptionless.io for documentation and support. +Please visit https://exceptionless.com/docs/ for documentation and support. diff --git a/src/Platforms/Exceptionless.Extensions.Logging/readme.txt b/src/Platforms/Exceptionless.Extensions.Logging/readme.txt index b695b6ab..adb56241 100644 --- a/src/Platforms/Exceptionless.Extensions.Logging/readme.txt +++ b/src/Platforms/Exceptionless.Extensions.Logging/readme.txt @@ -5,13 +5,13 @@ Exceptionless provides real-time error reporting for your apps. It organizes the gathered information into simple actionable data that will help your app become exceptionless! -Learn more at http://exceptionless.io. +Learn more at https://exceptionless.com/. ------------------------------------- How to get an api key ------------------------------------- The Exceptionless client requires an api key to use the Exceptionless service. -You can get your Exceptionless api key by logging into http://exceptionless.io +You can get your Exceptionless api key by logging into https://be.exceptionless.io/ and viewing your project configuration page. ------------------------------------- @@ -43,4 +43,4 @@ for examples on sending events to Exceptionless. ------------------------------------- Documentation and Support ------------------------------------- -Please visit http://exceptionless.io for documentation and support. +Please visit https://exceptionless.com/docs/ for documentation and support. diff --git a/src/Platforms/Exceptionless.Log4net/BufferingExceptionlessAppender.cs b/src/Platforms/Exceptionless.Log4net/BufferingExceptionlessAppender.cs index 6f359ae7..52bd35a3 100644 --- a/src/Platforms/Exceptionless.Log4net/BufferingExceptionlessAppender.cs +++ b/src/Platforms/Exceptionless.Log4net/BufferingExceptionlessAppender.cs @@ -5,6 +5,7 @@ namespace Exceptionless.Log4net { public class BufferingExceptionlessAppender : BufferingAppenderSkeleton { private ExceptionlessClient _client = ExceptionlessClient.Default; + private bool _ownsClient; public string ApiKey { get; set; } public string ServerUrl { get; set; } @@ -31,6 +32,7 @@ public override void ActivateOptions() { // Rely on Logging Rules config.SetDefaultMinLogLevel(Logging.LogLevel.Trace); }); + _ownsClient = true; } protected override void Append(LoggingEvent loggingEvent) { @@ -43,5 +45,18 @@ protected override void Append(LoggingEvent loggingEvent) { base.Append(loggingEvent); } + + public override bool Flush(int millisecondsTimeout) { + bool flushed = base.Flush(millisecondsTimeout); + return _client.ProcessQueueAsync().Wait(millisecondsTimeout) && flushed; + } + + protected override void OnClose() { + base.OnClose(); + + _client.ProcessQueueAsync().ConfigureAwait(false).GetAwaiter().GetResult(); + if (_ownsClient) + ((IDisposable)_client).Dispose(); + } } -} \ No newline at end of file +} diff --git a/src/Platforms/Exceptionless.Log4net/ExceptionlessAppender.cs b/src/Platforms/Exceptionless.Log4net/ExceptionlessAppender.cs index 9b5d0127..dc44aa33 100644 --- a/src/Platforms/Exceptionless.Log4net/ExceptionlessAppender.cs +++ b/src/Platforms/Exceptionless.Log4net/ExceptionlessAppender.cs @@ -5,6 +5,7 @@ namespace Exceptionless.Log4net { public class ExceptionlessAppender : AppenderSkeleton { private ExceptionlessClient _client = ExceptionlessClient.Default; + private bool _ownsClient; public string ApiKey { get; set; } public string ServerUrl { get; set; } @@ -24,6 +25,7 @@ public override void ActivateOptions() { // Rely on Logging Rules config.SetDefaultMinLogLevel(Logging.LogLevel.Trace); }); + _ownsClient = true; } protected override void Append(LoggingEvent loggingEvent) { @@ -36,5 +38,17 @@ protected override void Append(LoggingEvent loggingEvent) { _client.SubmitFromLogEvent(loggingEvent); } + + public override bool Flush(int millisecondsTimeout) { + return _client.ProcessQueueAsync().Wait(millisecondsTimeout); + } + + protected override void OnClose() { + base.OnClose(); + + _client.ProcessQueueAsync().ConfigureAwait(false).GetAwaiter().GetResult(); + if (_ownsClient) + ((IDisposable)_client).Dispose(); + } } } diff --git a/src/Platforms/Exceptionless.Log4net/readme.txt b/src/Platforms/Exceptionless.Log4net/readme.txt index ed821820..8e77effa 100644 --- a/src/Platforms/Exceptionless.Log4net/readme.txt +++ b/src/Platforms/Exceptionless.Log4net/readme.txt @@ -5,13 +5,13 @@ Exceptionless provides real-time error reporting for your apps. It organizes the gathered information into simple actionable data that will help your app become exceptionless! -Learn more at http://exceptionless.io. +Learn more at https://exceptionless.com/. ------------------------------------- How to get an api key ------------------------------------- The Exceptionless client requires an api key to use the Exceptionless service. -You can get your Exceptionless api key by logging into http://exceptionless.io +You can get your Exceptionless api key by logging into https://be.exceptionless.io/ and viewing your project configuration page. ------------------------------------- @@ -40,4 +40,4 @@ you can also set it on the appender like this: ------------------------------------- Documentation and Support ------------------------------------- -Please visit http://exceptionless.io for documentation and support. \ No newline at end of file +Please visit https://exceptionless.com/docs/ for documentation and support. \ No newline at end of file diff --git a/src/Platforms/Exceptionless.Mvc/readme.txt b/src/Platforms/Exceptionless.Mvc/readme.txt index 5175ade8..79efc629 100644 --- a/src/Platforms/Exceptionless.Mvc/readme.txt +++ b/src/Platforms/Exceptionless.Mvc/readme.txt @@ -5,13 +5,13 @@ Exceptionless provides real-time error reporting for your apps. It organizes the gathered information into simple actionable data that will help your app become exceptionless! -Learn more at http://exceptionless.io. +Learn more at https://exceptionless.com/. ------------------------------------- How to get an api key ------------------------------------- The Exceptionless client requires an api key to use the Exceptionless service. -You can get your Exceptionless api key by logging into http://exceptionless.io +You can get your Exceptionless api key by logging into https://be.exceptionless.io/ and viewing your project configuration page. ------------------------------------- @@ -47,4 +47,4 @@ exception.ToExceptionless().Submit() ------------------------------------- Documentation and Support ------------------------------------- -Please visit http://exceptionless.io for documentation and support. +Please visit https://exceptionless.com/docs/ for documentation and support. diff --git a/src/Platforms/Exceptionless.NLog/readme.txt b/src/Platforms/Exceptionless.NLog/readme.txt index a958c845..ad465d02 100644 --- a/src/Platforms/Exceptionless.NLog/readme.txt +++ b/src/Platforms/Exceptionless.NLog/readme.txt @@ -5,13 +5,13 @@ Exceptionless provides real-time error reporting for your apps. It organizes the gathered information into simple actionable data that will help your app become exceptionless! -Learn more at http://exceptionless.io. +Learn more at https://exceptionless.com/. ------------------------------------- How to get an api key ------------------------------------- The Exceptionless client requires an api key to use the Exceptionless service. -You can get your Exceptionless api key by logging into http://exceptionless.io +You can get your Exceptionless api key by logging into https://be.exceptionless.io/ and viewing your project configuration page. ------------------------------------- @@ -52,4 +52,4 @@ minimum log level that will be used until the client retrieves settings from the ------------------------------------- Documentation and Support ------------------------------------- -Please visit http://exceptionless.io for documentation and support. +Please visit https://exceptionless.com/docs/ for documentation and support. diff --git a/src/Platforms/Exceptionless.Web/readme.txt b/src/Platforms/Exceptionless.Web/readme.txt index 90ea2807..3aa8012d 100644 --- a/src/Platforms/Exceptionless.Web/readme.txt +++ b/src/Platforms/Exceptionless.Web/readme.txt @@ -5,13 +5,13 @@ Exceptionless provides real-time error reporting for your apps. It organizes the gathered information into simple actionable data that will help your app become exceptionless! -Learn more at http://exceptionless.io. +Learn more at https://exceptionless.com/. ------------------------------------- How to get an api key ------------------------------------- The Exceptionless client requires an api key to use the Exceptionless service. -You can get your Exceptionless api key by logging into http://exceptionless.io +You can get your Exceptionless api key by logging into https://be.exceptionless.io/ and viewing your project configuration page. ------------------------------------- @@ -52,4 +52,4 @@ exception.ToExceptionless().Submit() ------------------------------------- Documentation and Support ------------------------------------- -Please visit http://exceptionless.io for documentation and support. +Please visit https://exceptionless.com/docs/ for documentation and support. diff --git a/src/Platforms/Exceptionless.WebApi/readme.txt b/src/Platforms/Exceptionless.WebApi/readme.txt index 43241d1e..b3b4798a 100644 --- a/src/Platforms/Exceptionless.WebApi/readme.txt +++ b/src/Platforms/Exceptionless.WebApi/readme.txt @@ -5,13 +5,13 @@ Exceptionless provides real-time error reporting for your apps. It organizes the gathered information into simple actionable data that will help your app become exceptionless! -Learn more at http://exceptionless.io. +Learn more at https://exceptionless.com/. ------------------------------------- How to get an api key ------------------------------------- The Exceptionless client requires an api key to use the Exceptionless service. -You can get your Exceptionless api key by logging into http://exceptionless.io +You can get your Exceptionless api key by logging into https://be.exceptionless.io/ and viewing your project configuration page. ------------------------------------- @@ -65,4 +65,4 @@ exception.ToExceptionless().SetHttpActionContext(ActionContext).Submit() ------------------------------------- Documentation and Support ------------------------------------- -Please visit http://exceptionless.io for documentation and support. \ No newline at end of file +Please visit https://exceptionless.com/docs/ for documentation and support. \ No newline at end of file diff --git a/src/Platforms/Exceptionless.Windows/readme.txt b/src/Platforms/Exceptionless.Windows/readme.txt index d48743af..eb2f7219 100644 --- a/src/Platforms/Exceptionless.Windows/readme.txt +++ b/src/Platforms/Exceptionless.Windows/readme.txt @@ -5,13 +5,13 @@ Exceptionless provides real-time error reporting for your apps. It organizes the gathered information into simple actionable data that will help your app become exceptionless! -Learn more at http://exceptionless.io. +Learn more at https://exceptionless.com/. ------------------------------------- How to get an api key ------------------------------------- The Exceptionless client requires an api key to use the Exceptionless service. -You can get your Exceptionless api key by logging into http://exceptionless.io +You can get your Exceptionless api key by logging into https://be.exceptionless.io/ and viewing your project configuration page. ------------------------------------- @@ -71,4 +71,4 @@ ExceptionlessClient.Default.Configuration.SetUserIdentity("UNIQUE_ID_OR_EMAIL_AD ------------------------------------- Documentation and Support ------------------------------------- -Please visit http://exceptionless.io for documentation and support. +Please visit https://exceptionless.com/docs/ for documentation and support. diff --git a/src/Platforms/Exceptionless.Wpf/readme.txt b/src/Platforms/Exceptionless.Wpf/readme.txt index 59b28367..3d627842 100644 --- a/src/Platforms/Exceptionless.Wpf/readme.txt +++ b/src/Platforms/Exceptionless.Wpf/readme.txt @@ -5,13 +5,13 @@ Exceptionless provides real-time error reporting for your apps. It organizes the gathered information into simple actionable data that will help your app become exceptionless! -Learn more at http://exceptionless.io. +Learn more at https://exceptionless.com/. ------------------------------------- How to get an api key ------------------------------------- The Exceptionless client requires an api key to use the Exceptionless service. -You can get your Exceptionless api key by logging into http://exceptionless.io +You can get your Exceptionless api key by logging into https://be.exceptionless.io/ and viewing your project configuration page. ------------------------------------- @@ -70,4 +70,4 @@ ExceptionlessClient.Default.Configuration.SetUserIdentity("UNIQUE_ID_OR_EMAIL_AD ------------------------------------- Documentation and Support ------------------------------------- -Please visit http://exceptionless.io for documentation and support. +Please visit https://exceptionless.com/docs/ for documentation and support. diff --git a/test/Exceptionless.Tests/Configuration/ConfigurationTests.cs b/test/Exceptionless.Tests/Configuration/ConfigurationTests.cs index d369f64f..3d03182d 100644 --- a/test/Exceptionless.Tests/Configuration/ConfigurationTests.cs +++ b/test/Exceptionless.Tests/Configuration/ConfigurationTests.cs @@ -220,5 +220,26 @@ public void LogLevels_GetMinLogLevel_Settings_Order() { Assert.Equal(LogLevel.Info, settings.GetMinLogLevel("abc.def")); Assert.Equal(LogLevel.Trace, settings.GetMinLogLevel("abc.def.ghi")); } + + [Theory] + [InlineData("AWS_LAMBDA_FUNCTION_NAME", "my-function")] + [InlineData("FUNCTIONS_WORKER_RUNTIME", "dotnet")] + public void ReadFromEnvironmentalVariables_ServerlessEnvironment_EnablesProcessQueueOnCompletedRequest(string name, string value) { + var field = typeof(ExceptionlessConfigurationExtensions).GetField("_environmentVariables", BindingFlags.NonPublic | BindingFlags.Static); + Assert.NotNull(field); + var original = field.GetValue(null); + + try { + field.SetValue(null, new Dictionary { { name, value } }); + + var config = new ExceptionlessConfiguration(DependencyResolver.CreateDefault()); + Assert.False(config.ProcessQueueOnCompletedRequest); + + config.ReadFromEnvironmentalVariables(); + Assert.True(config.ProcessQueueOnCompletedRequest); + } finally { + field.SetValue(null, original); + } + } } } diff --git a/test/Exceptionless.Tests/Exceptionless.Tests.csproj b/test/Exceptionless.Tests/Exceptionless.Tests.csproj index 27cd3d6b..b406cddd 100644 --- a/test/Exceptionless.Tests/Exceptionless.Tests.csproj +++ b/test/Exceptionless.Tests/Exceptionless.Tests.csproj @@ -36,6 +36,7 @@ + diff --git a/test/Exceptionless.Tests/Platforms/Log4netAppenderTests.cs b/test/Exceptionless.Tests/Platforms/Log4netAppenderTests.cs new file mode 100644 index 00000000..ac471650 --- /dev/null +++ b/test/Exceptionless.Tests/Platforms/Log4netAppenderTests.cs @@ -0,0 +1,73 @@ +#if NET10_0_OR_GREATER +using System; +using System.Reflection; +using Exceptionless.Dependency; +using Exceptionless.Log4net; +using Exceptionless.Submission; +using Exceptionless.Tests.Utility; +using log4net.Appender; +using log4net.Core; +using Xunit; + +namespace Exceptionless.Tests.Platforms { + public class Log4netAppenderTests { + private const string ApiKey = "LhhP1C9gijpSKCslHHCvwdSIz298twx271nTest"; + + [Fact] + public void ExceptionlessAppender_Flush_SendsQueuedEvents() { + var appender = new ExceptionlessAppender { ApiKey = ApiKey }; + appender.ActivateOptions(); + var submissionClient = UseInMemorySubmissionClient(appender); + + appender.DoAppend(CreateLoggingEvent()); + + Assert.True(appender.Flush(5000)); + Assert.Single(submissionClient.Events); + appender.Close(); + } + + [Fact] + public void ExceptionlessAppender_Close_SendsQueuedEvents() { + var appender = new ExceptionlessAppender { ApiKey = ApiKey }; + appender.ActivateOptions(); + var submissionClient = UseInMemorySubmissionClient(appender); + + appender.DoAppend(CreateLoggingEvent()); + appender.Close(); + + Assert.Single(submissionClient.Events); + } + + [Fact] + public void BufferingExceptionlessAppender_Close_SendsBufferedEvents() { + var appender = new BufferingExceptionlessAppender { ApiKey = ApiKey, BufferSize = 10 }; + appender.ActivateOptions(); + var submissionClient = UseInMemorySubmissionClient(appender); + + appender.DoAppend(CreateLoggingEvent("first")); + appender.DoAppend(CreateLoggingEvent("second")); + Assert.Empty(submissionClient.Events); + + appender.Close(); + + Assert.Equal(2, submissionClient.Events.Count); + } + + private static InMemorySubmissionClient UseInMemorySubmissionClient(IAppender appender) { + var field = appender.GetType().GetField("_client", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(field); + var client = (ExceptionlessClient)field.GetValue(appender)!; + Assert.NotSame(ExceptionlessClient.Default, client); + + var submissionClient = new InMemorySubmissionClient(); + client.Configuration.Resolver.Register(submissionClient); + client.Configuration.UpdateSettingsWhenIdleInterval = TimeSpan.Zero; + return submissionClient; + } + + private static LoggingEvent CreateLoggingEvent(string message = "test message") { + return new LoggingEvent(typeof(Log4netAppenderTests), null, "Test", Level.Error, message, null); + } + } +} +#endif diff --git a/test/Exceptionless.Tests/Plugins/100_HeartbeatPluginTests.cs b/test/Exceptionless.Tests/Plugins/100_HeartbeatPluginTests.cs new file mode 100644 index 00000000..70a7a21a --- /dev/null +++ b/test/Exceptionless.Tests/Plugins/100_HeartbeatPluginTests.cs @@ -0,0 +1,26 @@ +using System; +using System.Reflection; +using Exceptionless.Plugins.Default; +using Xunit; + +namespace Exceptionless.Tests.Plugins { + public class HeartbeatPluginTests { + [Theory] + [InlineData(null, 60)] + [InlineData(10, 60)] + [InlineData(29, 60)] + [InlineData(30, 30)] + [InlineData(45, 45)] + [InlineData(75, 75)] + [InlineData(90, 90)] + [InlineData(120, 120)] + [InlineData(300, 300)] + public void Constructor_WithInterval_UsesIntervalWhenAtLeastThirtySeconds(int? seconds, int expectedSeconds) { + using var plugin = new HeartbeatPlugin(seconds.HasValue ? TimeSpan.FromSeconds(seconds.Value) : (TimeSpan?)null); + + var field = typeof(HeartbeatPlugin).GetField("_interval", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(field); + Assert.Equal(TimeSpan.FromSeconds(expectedSeconds), (TimeSpan)field.GetValue(plugin)!); + } + } +} diff --git a/test/Exceptionless.Tests/Queue/DefaultEventQueueTests.cs b/test/Exceptionless.Tests/Queue/DefaultEventQueueTests.cs new file mode 100644 index 00000000..67479514 --- /dev/null +++ b/test/Exceptionless.Tests/Queue/DefaultEventQueueTests.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Exceptionless.Dependency; +using Exceptionless.Logging; +using Exceptionless.Models; +using Exceptionless.Models.Data; +using Exceptionless.Queue; +using Exceptionless.Submission; +using Exceptionless.Tests.Log; +using Exceptionless.Tests.Utility; +using Xunit; + +namespace Exceptionless.Tests.Queue { + public class DefaultEventQueueTests { + private readonly TestOutputWriter _writer; + public DefaultEventQueueTests(ITestOutputHelper output) { + _writer = new TestOutputWriter(output); + } + + [Fact] + public async Task ProcessAsync_RequestEntityTooLarge_RetriesWithSmallerBatches() { + var submissionClient = new MaxBatchSizeSubmissionClient(10); + using var client = new ExceptionlessClient(c => { + c.UseLogger(new XunitExceptionlessLog(_writer) { MinimumLogLevel = LogLevel.Trace }); + c.ReadFromAttributes(); + c.UpdateSettingsWhenIdleInterval = TimeSpan.Zero; + c.Resolver.Register(submissionClient); + }); + + var queue = client.Configuration.Resolver.GetEventQueue(); + for (int i = 0; i < 50; i++) + queue.Enqueue(new Event { Type = Event.KnownTypes.Log, Message = i.ToString() }); + + await queue.ProcessAsync(); + + Assert.Equal(50, submissionClient.SubmittedEvents.Count); + Assert.Equal(50, submissionClient.SubmittedEvents.Distinct().Count()); + Assert.All(submissionClient.SubmittedBatchSizes, size => Assert.InRange(size, 1, 10)); + Assert.False(((DefaultEventQueue)queue).IsQueueProcessingSuspended); + } + + private class MaxBatchSizeSubmissionClient : ISubmissionClient { + private readonly int _maxBatchSize; + + public MaxBatchSizeSubmissionClient(int maxBatchSize) { + _maxBatchSize = maxBatchSize; + } + + public List SubmittedEvents { get; } = new List(); + public List SubmittedBatchSizes { get; } = new List(); + + public Task PostEventsAsync(IEnumerable events, ExceptionlessConfiguration config, IJsonSerializer serializer) { + var batch = events.ToList(); + if (batch.Count > _maxBatchSize) + return Task.FromResult(new SubmissionResponse(413, "Request Entity Too Large")); + + SubmittedBatchSizes.Add(batch.Count); + SubmittedEvents.AddRange(batch.Select(e => e.Message)); + return Task.FromResult(new SubmissionResponse(202, "Accepted")); + } + + public Task PostUserDescriptionAsync(string referenceId, UserDescription description, ExceptionlessConfiguration config, IJsonSerializer serializer) { + return Task.FromResult(new SubmissionResponse(200, "OK")); + } + + public Task GetSettingsAsync(ExceptionlessConfiguration config, int version, IJsonSerializer serializer) { + return Task.FromResult(new SettingsResponse(true)); + } + + public Task SendHeartbeatAsync(string sessionIdOrUserId, bool closeSession, ExceptionlessConfiguration config) { + return Task.CompletedTask; + } + } + } +}