mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 05:11:10 +00:00
Guards structured logging with IsEnabled checks
This commit is contained in:
1 parent
ccbd653ee5
commit
2e53449ada
44 files changed
+448
-136
No files matched your search
+22
@@ -41,6 +41,7 @@ public class ExceptionHandlingMiddlewareTests
|
||||
public ExceptionHandlingMiddlewareTests()
|
||||
{
|
||||
_mockLogger = Substitute.For<ILogger<ExceptionHandlingMiddleware>>();
|
||||
_mockLogger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);
|
||||
_mockStringLocalizer = Substitute.For<IStringLocalizer<ExceptionHandlingMiddleware>>();
|
||||
_mockStringLocalizer[Arg.Any<string>()].Returns(callInfo => new LocalizedString(callInfo.Arg<string>(), $"localized:{callInfo.Arg<string>()}"));
|
||||
_mockAuthenticationService = Substitute.For<IAuthenticationService>();
|
||||
@@ -91,6 +92,27 @@ public class ExceptionHandlingMiddlewareTests
|
||||
Arg.Any<Func<object, Exception?, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InvokeAsync_WhenLoggingIsDisabledAndNextThrowsNonApiExceptionAndApiRequest_ShouldNotLogError()
|
||||
{
|
||||
// Arrange
|
||||
_mockLogger.IsEnabled(Arg.Any<LogLevel>()).Returns(false);
|
||||
DefaultHttpContext httpContext = CreateHttpContext(isApiRequest: true);
|
||||
_nextDelegate = _ => throw new InvalidOperationException("Unexpected failure");
|
||||
|
||||
// Act
|
||||
await _sut.InvokeAsync(httpContext);
|
||||
|
||||
// Assert
|
||||
Assert.Contains("application/json", httpContext.Response.ContentType);
|
||||
_mockLogger.DidNotReceive().Log(
|
||||
LogLevel.Error,
|
||||
Arg.Any<EventId>(),
|
||||
Arg.Any<object>(),
|
||||
Arg.Any<Exception?>(),
|
||||
Arg.Any<Func<object, Exception?, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InvokeAsync_WhenNextThrowsNonApiExceptionAndPageRequest_ShouldWriteHtmlErrorPage()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user