Skip to content

Commit a52234e

Browse files
authored
Merge pull request #775 from leancodepl/feature/di-injected-logger-v2
DI-injected logger, attempt 2
2 parents a21da9b + 8cd227a commit a52234e

91 files changed

Lines changed: 628 additions & 239 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

LeanCode.CoreLibrary.sln

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeanCode.UserIdExtractors.T
191191
EndProject
192192
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeanCode.TestBed", "test-bed\LeanCode.TestBed.csproj", "{E0B2F070-425B-4453-80AE-EE23680F41D8}"
193193
EndProject
194+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeanCode.Logging.AspNetCore", "src\Infrastructure\LeanCode.Logging.AspNetCore\LeanCode.Logging.AspNetCore.csproj", "{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}"
195+
EndProject
196+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeanCode.Logging.AspNetCore.Tests", "test\Infrastructure\LeanCode.Logging.AspNetCore.Tests\LeanCode.Logging.AspNetCore.Tests.csproj", "{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}"
197+
EndProject
194198
Global
195199
GlobalSection(SolutionConfigurationPlatforms) = preSolution
196200
Debug|Any CPU = Debug|Any CPU
@@ -1137,6 +1141,30 @@ Global
11371141
{E0B2F070-425B-4453-80AE-EE23680F41D8}.Release|x64.Build.0 = Release|Any CPU
11381142
{E0B2F070-425B-4453-80AE-EE23680F41D8}.Release|x86.ActiveCfg = Release|Any CPU
11391143
{E0B2F070-425B-4453-80AE-EE23680F41D8}.Release|x86.Build.0 = Release|Any CPU
1144+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1145+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}.Debug|Any CPU.Build.0 = Debug|Any CPU
1146+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}.Debug|x64.ActiveCfg = Debug|Any CPU
1147+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}.Debug|x64.Build.0 = Debug|Any CPU
1148+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}.Debug|x86.ActiveCfg = Debug|Any CPU
1149+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}.Debug|x86.Build.0 = Debug|Any CPU
1150+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}.Release|Any CPU.ActiveCfg = Release|Any CPU
1151+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}.Release|Any CPU.Build.0 = Release|Any CPU
1152+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}.Release|x64.ActiveCfg = Release|Any CPU
1153+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}.Release|x64.Build.0 = Release|Any CPU
1154+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}.Release|x86.ActiveCfg = Release|Any CPU
1155+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92}.Release|x86.Build.0 = Release|Any CPU
1156+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1157+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}.Debug|Any CPU.Build.0 = Debug|Any CPU
1158+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}.Debug|x64.ActiveCfg = Debug|Any CPU
1159+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}.Debug|x64.Build.0 = Debug|Any CPU
1160+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}.Debug|x86.ActiveCfg = Debug|Any CPU
1161+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}.Debug|x86.Build.0 = Debug|Any CPU
1162+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}.Release|Any CPU.ActiveCfg = Release|Any CPU
1163+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}.Release|Any CPU.Build.0 = Release|Any CPU
1164+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}.Release|x64.ActiveCfg = Release|Any CPU
1165+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}.Release|x64.Build.0 = Release|Any CPU
1166+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}.Release|x86.ActiveCfg = Release|Any CPU
1167+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E}.Release|x86.Build.0 = Release|Any CPU
11401168
EndGlobalSection
11411169
GlobalSection(SolutionProperties) = preSolution
11421170
HideSolutionNode = FALSE
@@ -1245,5 +1273,7 @@ Global
12451273
{BAA648BD-0297-4D93-A169-EB2B8B286F30} = {E2F50E4D-58C4-4800-B031-D4E27636405E}
12461274
{29CC19F9-CF62-4613-B3BA-E7AAFEAE1BA2} = {9DC20510-2DB6-4B63-AAA7-C65A0B8F0157}
12471275
{8E57E9DE-108B-46D8-A7FF-65D28C21A3DF} = {29CC19F9-CF62-4613-B3BA-E7AAFEAE1BA2}
1276+
{F7C5AD4D-5A95-4399-9A67-1A4F332EBE92} = {1358A1B6-E8BB-4607-8B61-FDAE49F0EAA4}
1277+
{4B09532F-15A6-4D8A-B8B7-B53AB9AD2E0E} = {D9CFC5C4-8B87-4EDF-85D1-1471EA1B6D27}
12481278
EndGlobalSection
12491279
EndGlobal

docs/external_integrations/logging_serilog/index.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ Once configured, leverage Serilog for logging purposes in your application:
4545
```csharp
4646
public class UpdateProjectNameCH : ICommandHandler<UpdateProjectName>
4747
{
48-
private readonly Serilog.ILogger logger =
49-
Serilog.Log.ForContext<UpdateProjectNameCH>();
48+
private readonly ILogger<UpdateProjectNameCH> logger;
5049

5150
// . . .
5251

src/CQRS/LeanCode.CQRS.AspNetCore/LeanCode.CQRS.AspNetCore.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
<ItemGroup>
44
<ProjectReference Include="../LeanCode.CQRS.Annotations/LeanCode.CQRS.Annotations.csproj" />
5-
<ProjectReference Include="../LeanCode.CQRS.Execution/LeanCode.CQRS.Execution.csproj" />
5+
<ProjectReference Include="../LeanCode.CQRS.Execution/LeanCode.CQRS.Execution.csproj" />
66
<ProjectReference Include="../LeanCode.CQRS.Security/LeanCode.CQRS.Security.csproj" />
77
<ProjectReference Include="../../Core/LeanCode.Components/LeanCode.Components.csproj" />
8+
<ProjectReference Include="../../Infrastructure/LeanCode.Logging/LeanCode.Logging.csproj" />
89
<ProjectReference Include="../../Infrastructure/LeanCode.Serialization/LeanCode.Serialization.csproj" />
910
<ProjectReference Include="../../Infrastructure/LeanCode.OpenTelemetry/LeanCode.OpenTelemetry.csproj" />
1011
<ProjectReference Include="../LeanCode.CQRS.Validation/LeanCode.CQRS.Validation.csproj" />
1112
</ItemGroup>
1213

1314
<ItemGroup>
1415
<FrameworkReference Include="Microsoft.AspNetCore.App" />
15-
<PackageReference Include="Serilog" />
1616
</ItemGroup>
1717
</Project>

src/CQRS/LeanCode.CQRS.AspNetCore/Middleware/CQRSExceptionTranslationMiddleware.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22
using LeanCode.Contracts;
33
using LeanCode.Contracts.Validation;
44
using LeanCode.CQRS.Execution;
5+
using LeanCode.Logging;
56
using LeanCode.OpenTelemetry;
67
using Microsoft.AspNetCore.Http;
7-
using Serilog;
88

99
namespace LeanCode.CQRS.AspNetCore.Middleware;
1010

1111
public class CQRSExceptionTranslationMiddleware
1212
{
13-
private readonly ILogger logger = Log.ForContext<CQRSExceptionTranslationMiddleware>();
1413
private readonly CQRSMetrics metrics;
1514
private readonly RequestDelegate next;
15+
private readonly ILogger<CQRSExceptionTranslationMiddleware> logger;
1616

17-
public CQRSExceptionTranslationMiddleware(CQRSMetrics metrics, RequestDelegate next)
17+
public CQRSExceptionTranslationMiddleware(
18+
CQRSMetrics metrics,
19+
RequestDelegate next,
20+
ILogger<CQRSExceptionTranslationMiddleware> logger
21+
)
1822
{
1923
this.metrics = metrics;
2024
this.next = next;
25+
this.logger = logger;
2126
}
2227

2328
public async Task InvokeAsync(HttpContext httpContext)

src/CQRS/LeanCode.CQRS.AspNetCore/Middleware/CQRSMiddleware.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22
using System.Diagnostics.CodeAnalysis;
33
using LeanCode.CQRS.AspNetCore.Serialization;
44
using LeanCode.CQRS.Execution;
5+
using LeanCode.Logging;
56
using LeanCode.OpenTelemetry;
67
using Microsoft.AspNetCore.Http;
7-
using Serilog;
88

99
namespace LeanCode.CQRS.AspNetCore.Middleware;
1010

1111
public class CQRSMiddleware
1212
{
1313
private static readonly byte[] NullString = "null"u8.ToArray();
1414

15-
private readonly ILogger logger = Log.ForContext<CQRSMiddleware>();
16-
1715
private readonly CQRSMetrics metrics;
1816
private readonly ISerializer serializer;
1917
private readonly RequestDelegate next;
18+
private readonly ILogger<CQRSMiddleware> logger;
2019

21-
public CQRSMiddleware(CQRSMetrics metrics, ISerializer serializer, RequestDelegate next)
20+
public CQRSMiddleware(
21+
CQRSMetrics metrics,
22+
ISerializer serializer,
23+
RequestDelegate next,
24+
ILogger<CQRSMiddleware> logger
25+
)
2226
{
2327
this.metrics = metrics;
2428
this.serializer = serializer;
2529
this.next = next;
30+
this.logger = logger;
2631
}
2732

2833
[SuppressMessage("?", "CA1031", Justification = "The handler is an exception boundary.")]

src/CQRS/LeanCode.CQRS.AspNetCore/Middleware/CQRSSecurityMiddleware.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22
using LeanCode.Contracts.Security;
33
using LeanCode.CQRS.Execution;
44
using LeanCode.CQRS.Security;
5+
using LeanCode.Logging;
56
using LeanCode.OpenTelemetry;
67
using Microsoft.AspNetCore.Http;
7-
using Serilog;
88

99
namespace LeanCode.CQRS.AspNetCore.Middleware;
1010

1111
public class CQRSSecurityMiddleware
1212
{
1313
private readonly CQRSMetrics metrics;
1414
private readonly RequestDelegate next;
15-
private readonly ILogger logger = Log.ForContext<CQRSSecurityMiddleware>();
15+
private readonly ILogger<CQRSSecurityMiddleware> logger;
1616

17-
public CQRSSecurityMiddleware(CQRSMetrics metrics, RequestDelegate next)
17+
public CQRSSecurityMiddleware(CQRSMetrics metrics, RequestDelegate next, ILogger<CQRSSecurityMiddleware> logger)
1818
{
1919
this.metrics = metrics;
2020
this.next = next;
21+
this.logger = logger;
2122
}
2223

2324
public async Task InvokeAsync(HttpContext context)

src/CQRS/LeanCode.CQRS.AspNetCore/Middleware/CQRSValidationMiddleware.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22
using LeanCode.Contracts;
33
using LeanCode.CQRS.Execution;
44
using LeanCode.CQRS.Validation;
5+
using LeanCode.Logging;
56
using LeanCode.OpenTelemetry;
67
using Microsoft.AspNetCore.Http;
78

89
namespace LeanCode.CQRS.AspNetCore.Middleware;
910

1011
public class CQRSValidationMiddleware
1112
{
12-
private readonly Serilog.ILogger logger = Serilog.Log.ForContext<CQRSValidationMiddleware>();
13-
1413
private readonly CQRSMetrics metrics;
1514
private readonly RequestDelegate next;
15+
private readonly ILogger<CQRSValidationMiddleware> logger;
1616

17-
public CQRSValidationMiddleware(CQRSMetrics metrics, RequestDelegate next)
17+
public CQRSValidationMiddleware(CQRSMetrics metrics, RequestDelegate next, ILogger<CQRSValidationMiddleware> logger)
1818
{
1919
this.metrics = metrics;
2020
this.next = next;
21+
this.logger = logger;
2122
}
2223

2324
public async Task InvokeAsync(HttpContext httpContext, ICommandValidatorResolver resolver)

src/CQRS/LeanCode.CQRS.AspNetCore/Middleware/NonProductionResponseLoggerMiddleware.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
using LeanCode.CQRS.Execution;
2+
using LeanCode.Logging;
23
using Microsoft.AspNetCore.Http;
34
using Microsoft.Extensions.Hosting;
4-
using Serilog;
55

66
namespace LeanCode.CQRS.AspNetCore.Middleware;
77

88
public class NonProductionResponseLoggerMiddleware
99
{
10-
private readonly ILogger logger;
11-
private readonly IHostEnvironment environment;
10+
private readonly IHostEnvironment hostEnvironment;
11+
private readonly ILogger<NonProductionResponseLoggerMiddleware> logger;
1212

13-
public NonProductionResponseLoggerMiddleware(IHostEnvironment env)
13+
public NonProductionResponseLoggerMiddleware(
14+
IHostEnvironment hostEnvironment,
15+
ILogger<NonProductionResponseLoggerMiddleware> logger
16+
)
1417
{
15-
environment = env;
16-
logger = Log.ForContext<NonProductionResponseLoggerMiddleware>();
17-
}
18-
19-
public NonProductionResponseLoggerMiddleware(IHostEnvironment env, ILogger logger)
20-
{
21-
environment = env;
18+
this.hostEnvironment = hostEnvironment;
2219
this.logger = logger;
2320
}
2421

@@ -28,7 +25,7 @@ public async Task InvokeAsync(HttpContext httpContext, RequestDelegate next)
2825

2926
var payload = httpContext.GetCQRSRequestPayload();
3027

31-
if (!environment.IsProduction())
28+
if (!hostEnvironment.IsProduction())
3229
{
3330
logger.Information("Request executed with response {@Response}", payload.Result);
3431
}

src/CQRS/LeanCode.CQRS.AspNetCore/Middleware/ResponseLoggerMiddleware.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
using LeanCode.CQRS.Execution;
2+
using LeanCode.Logging;
23
using Microsoft.AspNetCore.Http;
3-
using Serilog;
44

55
namespace LeanCode.CQRS.AspNetCore.Middleware;
66

77
public class ResponseLoggerMiddleware
88
{
9-
private readonly ILogger logger;
109
private readonly RequestDelegate next;
10+
private readonly ILogger<ResponseLoggerMiddleware> logger;
1111

12-
public ResponseLoggerMiddleware(RequestDelegate next)
12+
public ResponseLoggerMiddleware(RequestDelegate next, ILogger<ResponseLoggerMiddleware> logger)
1313
{
14-
logger = Log.ForContext<ResponseLoggerMiddleware>();
15-
1614
this.next = next;
15+
this.logger = logger;
1716
}
1817

1918
public async Task InvokeAsync(HttpContext httpContext)

src/CQRS/LeanCode.CQRS.MassTransitRelay/LeanCode.CQRS.MassTransitRelay.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
<ItemGroup>
44
<ProjectReference Include="../../Domain/LeanCode.DomainModels/LeanCode.DomainModels.csproj" />
5+
<ProjectReference Include="../../Infrastructure/LeanCode.Logging/LeanCode.Logging.csproj" />
6+
57
</ItemGroup>
68

79
<ItemGroup>
@@ -11,6 +13,5 @@
1113
<PackageReference Include="Microsoft.Extensions.Hosting" />
1214
<PackageReference Include="Microsoft.EntityFrameworkCore" />
1315
<PackageReference Include="MassTransit.EntityFrameworkCore" />
14-
<PackageReference Include="Serilog" />
1516
</ItemGroup>
1617
</Project>

0 commit comments

Comments
 (0)