public void CanCallEndBatchWithoutStartBatch() { BatchLogger.EndBatch(); UT.Assert.AreEqual(0, _loggedRecords); UT.Assert.AreEqual(0, _callsToFallback); _mockLogger.Verify(); }
public void LogAllThenLogSome() { FulcrumApplication.Setup.LogSeverityLevelThreshold = LogSeverityLevel.Warning; BatchLogger.StartBatch(LogSeverityLevel.Error, false); Log.LogVerbose("Verbose"); Log.LogInformation("Information"); Log.LogWarning("Warning"); UT.Assert.AreEqual(0, _loggedRecords); Log.LogError("Error"); UT.Assert.AreEqual(4, _loggedRecords); Log.LogVerbose("Verbose"); UT.Assert.AreEqual(5, _loggedRecords); BatchLogger.EndBatch(); _loggedRecords = 0; BatchLogger.StartBatch(LogSeverityLevel.Error, false); Log.LogVerbose("Verbose"); Log.LogInformation("Information"); Log.LogWarning("Warning"); UT.Assert.AreEqual(0, _loggedRecords); BatchLogger.EndBatch(); UT.Assert.AreEqual(1, _loggedRecords); UT.Assert.AreEqual(0, _callsToFallback); _mockLogger.Verify(); }
public void HandlesUnderlyingCollectionProperly() { FulcrumApplication.Setup.LogSeverityLevelThreshold = LogSeverityLevel.Warning; BatchLogger.StartBatch(LogSeverityLevel.Warning, true); for (var i = 1; i <= 10; i++) { Log.LogWarning($"Warning A {i}"); } var allDone = new ManualResetEvent(false); // Try to provoke "System.InvalidOperationException: Collection was modified; enumeration operation may not execute." // Prior to version 4.15.3 this would trigger the exception: ThreadHelper.FireAndForget(() => { for (var i = 1; i <= 100000; i++) { Log.LogWarning($"Warning B {i}"); } allDone.Set(); }); BatchLogger.EndBatch(); UT.Assert.IsTrue(allDone.WaitOne(TimeSpan.FromSeconds(10))); UT.Assert.AreEqual(0, _callsToFallback, $"{nameof(_loggedRecords)}: {_loggedRecords}"); UT.Assert.IsTrue(_loggedRecords >= 100, _loggedRecords.ToString()); }
public void LogOnlyOverThreshold() { FulcrumApplication.Setup.LogSeverityLevelThreshold = LogSeverityLevel.Warning; BatchLogger.StartBatch(LogSeverityLevel.None, true); Log.LogVerbose("Verbose"); Log.LogInformation("Information"); Log.LogWarning("Warning"); Log.LogError("Error"); Log.LogCritical("Critical"); UT.Assert.AreEqual(0, _loggedRecords); BatchLogger.EndBatch(); UT.Assert.AreEqual(3, _loggedRecords); UT.Assert.AreEqual(0, _callsToFallback); _mockLogger.Verify(); }
// TODO: Make code example complete // TODO: Make callbacks in options // TODO: Move code into one big invoke // ReSharper disable once UnusedMember.Global /// <summary> /// The main method for a middleware. /// </summary> /// <param name="context">The information about the current HTTP request.</param> public virtual async Task InvokeAsync(HttpContext context) { var stopwatch = new Stopwatch(); stopwatch.Start(); var cancellationToken = context.RequestAborted; // Enable multiple reads of the content context.Request.EnableBuffering(); try { if (Options.Features.SaveClientTenant.Enabled) { var tenant = GetClientTenantFromUrl(context); FulcrumApplication.Context.ClientTenant = tenant; } if (Options.Features.SaveCorrelationId.Enabled) { var correlationId = GetOrCreateCorrelationId(context); FulcrumApplication.Context.CorrelationId = correlationId; } if (Options.Features.SaveExecutionId.Enabled) { var parentExecutionId = ExtractParentExecutionIdFromHeader(context); FulcrumApplication.Context.ParentExecutionId = parentExecutionId; var executionId = ExtractExecutionIdFromHeader(context); FulcrumApplication.Context.ExecutionId = executionId; } if (Options.Features.SaveTenantConfiguration.Enabled) { var tenantConfiguration = await GetTenantConfigurationAsync(FulcrumApplication.Context.ClientTenant, context, cancellationToken); FulcrumApplication.Context.LeverConfiguration = tenantConfiguration; } if (Options.Features.SaveNexusTestContext.Enabled) { var testContext = GetNexusTestContextFromHeader(context); FulcrumApplication.Context.NexusTestContext = testContext; } if (Options.Features.BatchLog.Enabled) { BatchLogger.StartBatch(Options.Features.BatchLog.Threshold, Options.Features.BatchLog.FlushAsLateAsPossible); } try { await Next(context); if (Options.Features.LogRequestAndResponse.Enabled) { await LogResponseAsync(context, stopwatch.Elapsed, cancellationToken); } } catch (Exception exception) { var shouldThrow = true; if (Options.Features.ConvertExceptionToHttpResponse.Enabled) { await ConvertExceptionToResponseAsync(context, exception, cancellationToken); shouldThrow = false; if (Options.Features.LogRequestAndResponse.Enabled) { await LogResponseAsync(context, stopwatch.Elapsed, cancellationToken); } } if (shouldThrow) { throw; } } } catch (Exception exception) { if (Options.Features.LogRequestAndResponse.Enabled) { LogException(context, exception, stopwatch.Elapsed); } throw; } finally { if (Options.Features.BatchLog.Enabled) { BatchLogger.EndBatch(); } } }