Skip to content
Commits on Source (1)
......@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.2.13</Version>
<Version>1.3.0</Version>
<Copyright>Catharsium</Copyright>
<PackageProjectUrl>https://gitlab.com/Catharsium/Catharsium.Util</PackageProjectUrl>
<RepositoryUrl>https://gitlab.com/Catharsium/Catharsium.Util</RepositoryUrl>
......
......@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.2.13</Version>
<Version>1.3.0</Version>
<Copyright>Catharsium</Copyright>
<PackageProjectUrl>https://gitlab.com/Catharsium/Catharsium.Util</PackageProjectUrl>
<RepositoryUrl>https://gitlab.com/Catharsium/Catharsium.Util</RepositoryUrl>
......
......@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.2.13</Version>
<Version>1.3.0</Version>
<Copyright>Catharsium</Copyright>
<PackageProjectUrl>https://gitlab.com/Catharsium/Catharsium.Util</PackageProjectUrl>
<RepositoryUrl>https://gitlab.com/Catharsium/Catharsium.Util</RepositoryUrl>
......
......@@ -13,21 +13,41 @@ namespace Catharsium.Util.Web.Tests.Middleware.Logging
public class ErrorLoggingHandlerTests : TestFixture<ErrorLoggingHandler>
{
[TestMethod]
public void Invoke_LogsError()
public void Invoke_ExceptionInLifecycle_LogsError()
{
var logger = Substitute.For<ILogger<ErrorLoggingHandler>>();
var httpContext = Substitute.For<HttpContext>();
this.SetDependency<RequestDelegate>(this.MyRequestDelegate);
var logger = Substitute.For<ILogger<ErrorLoggingHandler>>();
this.SetDependency(logger);
this.SetDependency<RequestDelegate>(this.MyRequestDelegateWithException);
var task = this.Target.Invoke(httpContext, logger);
Assert.ThrowsException<ApplicationException>(() => task.GetAwaiter().GetResult());
var actual = this.Target.Invoke(httpContext);
Assert.ThrowsException<ApplicationException>(() => actual.GetAwaiter().GetResult());
logger.ReceivedWithAnyArgs(1).LogError(Arg.Any<ApplicationException>(), null);
}
public Task MyRequestDelegate(HttpContext context)
[TestMethod]
public void Invoke_NoException_DoesNotLog()
{
var httpContext = Substitute.For<HttpContext>();
var logger = Substitute.For<ILogger<ErrorLoggingHandler>>();
this.SetDependency(logger);
this.SetDependency<RequestDelegate>(this.MyHealthyRequestDelegate);
var actual = this.Target.Invoke(httpContext);
logger.DidNotReceiveWithAnyArgs().Log(Arg.Any<LogLevel>(), Arg.Any<Exception>(), null);
}
public Task MyRequestDelegateWithException(HttpContext context)
{
throw new ApplicationException();
}
public Task MyHealthyRequestDelegate(HttpContext context)
{
return Task.CompletedTask;
}
}
}
\ No newline at end of file
......@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.2.13</Version>
<Version>1.3.0</Version>
<PackageProjectUrl>https://gitlab.com/Catharsium/Catharsium.Util</PackageProjectUrl>
<Copyright>Catharsium</Copyright>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
......
......@@ -7,25 +7,25 @@ namespace Catharsium.Util.Web.Middleware.Logging
{
public class ErrorLoggingHandler
{
private readonly ILogger<ErrorLoggingHandler> log;
private readonly RequestDelegate next;
public ErrorLoggingHandler(RequestDelegate next)
public ErrorLoggingHandler(ILogger<ErrorLoggingHandler> log, RequestDelegate next)
{
this.log = log;
this.next = next;
}
public async Task Invoke(HttpContext httpContext, ILogger<ErrorLoggingHandler> logger)
public async Task Invoke(HttpContext httpContext)
{
try
{
try {
await this.next(httpContext);
}
catch (Exception ex)
{
logger.LogError(ex, "Uncaught exception");
catch (Exception ex) {
this.log.LogError(ex, null);
throw;
}
}
......
......@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.2.13</Version>
<Version>1.3.0</Version>
<PackageProjectUrl>https://gitlab.com/Catharsium/Catharsium.Util</PackageProjectUrl>
<Copyright>Catharsium</Copyright>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
......