Skip to content

Commit febf135

Browse files
committed
Add LogAndReturn Definitions
1 parent 78b5bfb commit febf135

File tree

5 files changed

+182
-1
lines changed

5 files changed

+182
-1
lines changed

src/AStar.Dev.Logging.Extensions/AStar.Dev.Logging.Extensions.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2727
<TargetFramework>net9.0</TargetFramework>
2828
<Title>AStar.Dev.Logging.Extensions</Title>
29-
<Version>0.5.2</Version>
29+
<Version>0.5.3</Version>
3030
</PropertyGroup>
3131

3232

@@ -42,6 +42,8 @@
4242
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0"/>
4343
<PackageReference Include="AStar.Dev.Technical.Debt.Reporting" Version="0.1.0"/>
4444
<PackageReference Include="AStar.Dev.Utilities" Version="1.6.0"/>
45+
<PackageReference Include="AStar.Dev.Functional.Extensions" Version="0.2.0"/>
46+
<PackageReference Include="AStar.Dev.Api.HealthChecks" Version="0.4.0"/>
4547
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3"/>
4648
<PackageReference Include="Serilog.Enrichers.Span" Version="3.1.0"/>
4749
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1"/>

src/AStar.Dev.Logging.Extensions/AStar.Dev.Logging.Extensions.xml

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AStar.Dev.Logging.Extensions/AStarLogger.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using AStar.Dev.Api.HealthChecks;
2+
using AStar.Dev.Functional.Extensions;
3+
14
namespace AStar.Dev.Logging.Extensions;
25

36
/// <summary>
@@ -57,6 +60,30 @@ public void LogApiCallWarning(string apiName, string uri, string warningMessage)
5760
public void LogApiCallFailed(string apiName, string uri, string failureMessage)
5861
=> LoggerMessageDefinitionsApi.ApiCallFailure(logger, apiName, uri, failureMessage, null);
5962

63+
/// <inheritdoc />
64+
public HealthStatusResponse ReturnLoggedFailure(HttpResponseMessage response, string apiName)
65+
{
66+
LogHealthCheckFailure(apiName, response.ReasonPhrase ?? "Failure reason not known");
67+
68+
return new() { Status = $"Health Check failed - {response.ReasonPhrase}." };
69+
}
70+
71+
/// <inheritdoc />
72+
public T ReturnLoggedSuccess<T>(T result, string apiName, string endpointName)
73+
{
74+
LogApiCallSuccess(apiName, endpointName);
75+
76+
return result;
77+
}
78+
79+
/// <inheritdoc />
80+
public Result<string, T> ReturnLoggedFailure<T>(string apiName, string endpointName, string failureMessage)
81+
{
82+
LogApiCallFailed(apiName, endpointName, failureMessage);
83+
84+
return Result<string, T>.Failure($"Call to {endpointName} failed with {failureMessage}")!;
85+
}
86+
6087
/// <inheritdoc />
6188
public IDisposable? BeginScope<TState>(TState state)
6289
where TState : notnull

src/AStar.Dev.Logging.Extensions/ILoggerAstar.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using AStar.Dev.Api.HealthChecks;
2+
using AStar.Dev.Functional.Extensions;
3+
14
namespace AStar.Dev.Logging.Extensions;
25

36
/// <summary>
@@ -79,4 +82,32 @@ public interface ILoggerAstar<out T> : ILogger<T>
7982
/// <param name="uri">The URI being called</param>
8083
/// <param name="failureMessage">The failure message to log</param>
8184
void LogApiCallFailed(string apiName, string uri, string failureMessage);
85+
86+
/// <summary>
87+
/// The ReturnLoggedFailure will log the Health Check failure and return the <see cref="HealthStatusResponse"/> object
88+
/// </summary>
89+
/// <param name="response">The instance of <see cref="HttpResponseMessage"/> from the original call</param>
90+
/// <param name="apiName">The name of the API being called</param>
91+
/// <returns>An instance of <see cref="HealthStatusResponse"/> appropriately configured</returns>
92+
HealthStatusResponse ReturnLoggedFailure(HttpResponseMessage response, string apiName);
93+
94+
/// <summary>
95+
/// The ReturnLoggedSuccess method will log the successful call and return the TResult response object
96+
/// </summary>
97+
/// <param name="result"></param>
98+
/// <param name="apiName">The name of the API being called</param>
99+
/// <param name="endpointName">The endpoint name of the call</param>
100+
/// <typeparam name="TResult"></typeparam>
101+
/// <returns>An instance of TResult appropriately configured</returns>
102+
TResult ReturnLoggedSuccess<TResult>(TResult result, string apiName, string endpointName);
103+
104+
/// <summary>
105+
/// The ReturnLoggedFailure method will log the failed call and return the TResult response object
106+
/// </summary>
107+
/// <param name="apiName">The name of the API being called</param>
108+
/// <param name="endpointName">The endpoint name of the call</param>
109+
/// <param name="failureMessage">A preconfigured error message to log</param>
110+
/// <typeparam name="TResult">The type of the result</typeparam>
111+
/// <returns>An instance of Result{string,TResult} appropriately configured</returns>
112+
Result<string, TResult> ReturnLoggedFailure<TResult>(string apiName, string endpointName, string failureMessage);
82113
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Reflection;
2+
using AStar.Dev.Logging.Extensions;
3+
using JetBrains.Annotations;
4+
using Microsoft.Extensions.Logging;
5+
using Microsoft.Extensions.Logging.Abstractions;
6+
using Xunit.Abstractions;
7+
8+
namespace AStar.Dev.Logging.Extensions;
9+
10+
[TestSubject(typeof(LoggerMessageDefinitionsApi))]
11+
public class LoggerMessageDefinitionsApiShould
12+
{
13+
[Fact]
14+
public void DefineTheHealthCheckFailureMessageAsExpected()
15+
{
16+
var healthCheckFailure = LoggerMessageDefinitionsApi.HealthCheckFailure;
17+
18+
healthCheckFailure.Method.ToString().ShouldBe("Void <Define>b__1(Microsoft.Extensions.Logging.ILogger, System.String, System.String, System.Exception)");
19+
healthCheckFailure.Target?.ToString().ShouldBe("Microsoft.Extensions.Logging.LoggerMessage+<>c__DisplayClass12_0`2[System.String,System.String]");
20+
}
21+
22+
[Fact]
23+
public void DefineTheHealthCheckStartMessageAsExpected()
24+
{
25+
var healthCheckStart = LoggerMessageDefinitionsApi.HealthCheckStart;
26+
27+
healthCheckStart.Method.ToString().ShouldBe("Void <Define>b__1(Microsoft.Extensions.Logging.ILogger, System.String, System.Exception)");
28+
healthCheckStart.Target?.ToString().ShouldBe("Microsoft.Extensions.Logging.LoggerMessage+<>c__DisplayClass10_0`1[System.String]");
29+
}
30+
31+
[Fact]
32+
public void DefineTheHealthCheckSuccessMessageAsExpected()
33+
{
34+
var healthCheckSuccess = LoggerMessageDefinitionsApi.HealthCheckSuccess;
35+
36+
healthCheckSuccess.Method.ToString().ShouldBe("Void <Define>b__1(Microsoft.Extensions.Logging.ILogger, System.String, System.Exception)");
37+
healthCheckSuccess.Target?.ToString().ShouldBe("Microsoft.Extensions.Logging.LoggerMessage+<>c__DisplayClass10_0`1[System.String]");
38+
}
39+
40+
[Fact]
41+
public void DefineTheHealthCheckWarningMessageAsExpected()
42+
{
43+
var healthCheckWarning = LoggerMessageDefinitionsApi.HealthCheckWarning;
44+
45+
healthCheckWarning.Method.ToString().ShouldBe("Void <Define>b__1(Microsoft.Extensions.Logging.ILogger, System.String, System.String, System.Exception)");
46+
healthCheckWarning.Target?.ToString().ShouldBe("Microsoft.Extensions.Logging.LoggerMessage+<>c__DisplayClass12_0`2[System.String,System.String]");
47+
}
48+
49+
[Fact]
50+
public void DefineTheApiCallFailureMessageAsExpected()
51+
{
52+
var apiCallFailure = LoggerMessageDefinitionsApi.ApiCallFailure;
53+
54+
apiCallFailure.Method.ToString().ShouldBe("Void <Define>b__1(Microsoft.Extensions.Logging.ILogger, System.String, System.String, System.String, System.Exception)");
55+
apiCallFailure.Target?.ToString().ShouldBe("Microsoft.Extensions.Logging.LoggerMessage+<>c__DisplayClass14_0`3[System.String,System.String,System.String]");
56+
}
57+
58+
[Fact]
59+
public void DefineTheApiCallStartMessageAsExpected()
60+
{
61+
var apiCallStart = LoggerMessageDefinitionsApi.ApiCallStart;
62+
63+
apiCallStart.Method.ToString().ShouldBe("Void <Define>b__1(Microsoft.Extensions.Logging.ILogger, System.String, System.String, System.Exception)");
64+
apiCallStart.Target?.ToString().ShouldBe("Microsoft.Extensions.Logging.LoggerMessage+<>c__DisplayClass12_0`2[System.String,System.String]");
65+
}
66+
67+
[Fact]
68+
public void DefineTheApiCallSuccessMessageAsExpected()
69+
{
70+
var apiCallSuccess = LoggerMessageDefinitionsApi.ApiCallSuccess;
71+
72+
apiCallSuccess.Method.ToString().ShouldBe("Void <Define>b__1(Microsoft.Extensions.Logging.ILogger, System.String, System.String, System.Exception)");
73+
apiCallSuccess.Target?.ToString().ShouldBe("Microsoft.Extensions.Logging.LoggerMessage+<>c__DisplayClass12_0`2[System.String,System.String]");
74+
}
75+
76+
[Fact]
77+
public void DefineTheApiCallWarningMessageAsExpected()
78+
{
79+
var apiCallWarning = LoggerMessageDefinitionsApi.ApiCallWarning;
80+
81+
apiCallWarning.Method.ToString().ShouldBe("Void <Define>b__1(Microsoft.Extensions.Logging.ILogger, System.String, System.String, System.String, System.Exception)");
82+
apiCallWarning.Target?.ToString().ShouldBe("Microsoft.Extensions.Logging.LoggerMessage+<>c__DisplayClass14_0`3[System.String,System.String,System.String]");
83+
}
84+
}

0 commit comments

Comments
 (0)