Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CORELIB-75] DI injected logger #757

Open
wants to merge 11 commits into
base: v9.0-preview
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/external_integrations/logging_serilog/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ Once configured, leverage Serilog for logging purposes in your application:
```csharp
public class UpdateProjectNameCH : ICommandHandler<UpdateProjectName>
{
private readonly Serilog.ILogger logger =
Serilog.Log.ForContext<UpdateProjectNameCH>();
private readonly ILogger<UpdateProjectNameCH> logger;

// . . .

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

<ItemGroup>
<ProjectReference Include="../LeanCode.CQRS.Annotations/LeanCode.CQRS.Annotations.csproj" />
<ProjectReference Include="../LeanCode.CQRS.Execution/LeanCode.CQRS.Execution.csproj" />
<ProjectReference Include="../LeanCode.CQRS.Execution/LeanCode.CQRS.Execution.csproj" />
<ProjectReference Include="../LeanCode.CQRS.Security/LeanCode.CQRS.Security.csproj" />
<ProjectReference Include="../../Core/LeanCode.Components/LeanCode.Components.csproj" />
<ProjectReference Include="../../Infrastructure/LeanCode.Logging/LeanCode.Logging.csproj" />
<ProjectReference Include="../../Infrastructure/LeanCode.Serialization/LeanCode.Serialization.csproj" />
<ProjectReference Include="../../Infrastructure/LeanCode.OpenTelemetry/LeanCode.OpenTelemetry.csproj" />
<ProjectReference Include="../LeanCode.CQRS.Validation/LeanCode.CQRS.Validation.csproj" />
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Serilog" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
using LeanCode.Contracts;
using LeanCode.Contracts.Validation;
using LeanCode.CQRS.Execution;
using LeanCode.Logging;
using LeanCode.OpenTelemetry;
using Microsoft.AspNetCore.Http;
using Serilog;

namespace LeanCode.CQRS.AspNetCore.Middleware;

public class CQRSExceptionTranslationMiddleware
{
private readonly ILogger logger = Log.ForContext<CQRSExceptionTranslationMiddleware>();
private readonly ILogger<CQRSExceptionTranslationMiddleware> logger;
private readonly CQRSMetrics metrics;
private readonly RequestDelegate next;

public CQRSExceptionTranslationMiddleware(CQRSMetrics metrics, RequestDelegate next)
public CQRSExceptionTranslationMiddleware(
ILogger<CQRSExceptionTranslationMiddleware> logger,
CQRSMetrics metrics,
RequestDelegate next
)
{
this.logger = logger;
this.metrics = metrics;
this.next = next;
}
Expand Down
12 changes: 9 additions & 3 deletions src/CQRS/LeanCode.CQRS.AspNetCore/Middleware/CQRSMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
using System.Diagnostics.CodeAnalysis;
using LeanCode.CQRS.AspNetCore.Serialization;
using LeanCode.CQRS.Execution;
using LeanCode.Logging;
using Microsoft.AspNetCore.Http;
using Serilog;

namespace LeanCode.CQRS.AspNetCore.Middleware;

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

private readonly ILogger logger = Log.ForContext<CQRSMiddleware>();
private readonly ILogger<CQRSMiddleware> logger;

private readonly CQRSMetrics metrics;
private readonly ISerializer serializer;
private readonly RequestDelegate next;

public CQRSMiddleware(CQRSMetrics metrics, ISerializer serializer, RequestDelegate next)
public CQRSMiddleware(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last (not priority, not mandatory) request - let's put the logger as a last parameter (I think this is also a convention in the .NET examples, but might be wrong here).

ILogger<CQRSMiddleware> logger,
CQRSMetrics metrics,
ISerializer serializer,
RequestDelegate next
)
{
this.logger = logger;
this.metrics = metrics;
this.serializer = serializer;
this.next = next;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using LeanCode.Contracts.Security;
using LeanCode.CQRS.Execution;
using LeanCode.CQRS.Security;
using LeanCode.Logging;
using LeanCode.OpenTelemetry;
using Microsoft.AspNetCore.Http;
using Serilog;

namespace LeanCode.CQRS.AspNetCore.Middleware;

public class CQRSSecurityMiddleware
{
private readonly CQRSMetrics metrics;
private readonly RequestDelegate next;
private readonly ILogger logger = Log.ForContext<CQRSSecurityMiddleware>();
private readonly ILogger<CQRSSecurityMiddleware> logger;

public CQRSSecurityMiddleware(CQRSMetrics metrics, RequestDelegate next)
public CQRSSecurityMiddleware(CQRSMetrics metrics, RequestDelegate next, ILogger<CQRSSecurityMiddleware> logger)
{
this.metrics = metrics;
this.next = next;
this.logger = logger;
}

public async Task InvokeAsync(HttpContext context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using LeanCode.Contracts;
using LeanCode.CQRS.Execution;
using LeanCode.CQRS.Validation;
using LeanCode.Logging;
using LeanCode.OpenTelemetry;
using Microsoft.AspNetCore.Http;

namespace LeanCode.CQRS.AspNetCore.Middleware;

public class CQRSValidationMiddleware
{
private readonly Serilog.ILogger logger = Serilog.Log.ForContext<CQRSValidationMiddleware>();
private readonly ILogger<CQRSValidationMiddleware> logger;

private readonly CQRSMetrics metrics;
private readonly RequestDelegate next;

public CQRSValidationMiddleware(CQRSMetrics metrics, RequestDelegate next)
public CQRSValidationMiddleware(ILogger<CQRSValidationMiddleware> logger, CQRSMetrics metrics, RequestDelegate next)
{
this.logger = logger;
this.metrics = metrics;
this.next = next;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
using LeanCode.CQRS.Execution;
using LeanCode.Logging;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using Serilog;

namespace LeanCode.CQRS.AspNetCore.Middleware;

public class NonProductionResponseLoggerMiddleware
{
private readonly ILogger logger;
private readonly ILogger<NonProductionResponseLoggerMiddleware> logger;
private readonly IHostEnvironment environment;

public NonProductionResponseLoggerMiddleware(IHostEnvironment env)
public NonProductionResponseLoggerMiddleware(
ILogger<NonProductionResponseLoggerMiddleware> logger,
IHostEnvironment env
)
{
environment = env;
logger = Log.ForContext<NonProductionResponseLoggerMiddleware>();
}

public NonProductionResponseLoggerMiddleware(IHostEnvironment env, ILogger logger)
{
environment = env;
this.logger = logger;
environment = env;
}

public async Task InvokeAsync(HttpContext httpContext, RequestDelegate next)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using LeanCode.CQRS.Execution;
using LeanCode.Logging;
using Microsoft.AspNetCore.Http;
using Serilog;

namespace LeanCode.CQRS.AspNetCore.Middleware;

public class ResponseLoggerMiddleware
{
private readonly ILogger logger;
private readonly ILogger<ResponseLoggerMiddleware> logger;
private readonly RequestDelegate next;

public ResponseLoggerMiddleware(RequestDelegate next)
public ResponseLoggerMiddleware(ILogger<ResponseLoggerMiddleware> logger, RequestDelegate next)
{
logger = Log.ForContext<ResponseLoggerMiddleware>();

this.logger = logger;
this.next = next;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<ItemGroup>
<ProjectReference Include="../../Domain/LeanCode.DomainModels/LeanCode.DomainModels.csproj" />
<ProjectReference Include="../../Infrastructure/LeanCode.Logging/LeanCode.Logging.csproj" />

</ItemGroup>

<ItemGroup>
Expand All @@ -11,6 +13,5 @@
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Microsoft.EntityFrameworkCore" />
<PackageReference Include="MassTransit.EntityFrameworkCore" />
<PackageReference Include="Serilog" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
using LeanCode.DomainModels.Model;
using LeanCode.Logging;
using MassTransit;
using Microsoft.AspNetCore.Http;

namespace LeanCode.CQRS.MassTransitRelay.Middleware;

public class EventsPublisherMiddleware
{
private readonly Serilog.ILogger logger = Serilog.Log.ForContext<EventsPublisherMiddleware>();
private readonly ILogger<EventsPublisherMiddleware> logger;
private readonly RequestDelegate next;
private readonly AsyncEventsInterceptor interceptor;

public EventsPublisherMiddleware(RequestDelegate next, AsyncEventsInterceptor interceptor)
public EventsPublisherMiddleware(
ILogger<EventsPublisherMiddleware> logger,
RequestDelegate next,
AsyncEventsInterceptor interceptor
)
{
this.logger = logger;
this.next = next;
this.interceptor = interceptor;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using System.Security.Claims;
using LeanCode.Contracts.Security;
using LeanCode.Logging;

namespace LeanCode.CQRS.Security;

public class DefaultPermissionAuthorizer : CustomAuthorizer<object, string[]>, IHasPermissions
{
private readonly Serilog.ILogger logger = Serilog.Log.ForContext<DefaultPermissionAuthorizer>();
private readonly ILogger<DefaultPermissionAuthorizer> logger;

private readonly RoleRegistry registry;

public DefaultPermissionAuthorizer(RoleRegistry registry)
public DefaultPermissionAuthorizer(ILogger<DefaultPermissionAuthorizer> logger, RoleRegistry registry)
{
this.logger = logger;
this.registry = registry;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="../../Infrastructure/LeanCode.Logging/LeanCode.Logging.csproj" />
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="LeanCode.Contracts" />
<PackageReference Include="Serilog" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using Serilog;
using LeanCode.Logging;

namespace LeanCode.AuditLogs;

public class AzureBlobAuditLogStorage : IAuditLogStorage
{
private const string SuffixKey = "Suffix";
private readonly ILogger logger = Log.ForContext<AzureBlobAuditLogStorage>();
private readonly ILogger<AzureBlobAuditLogStorage> logger;

private static ReadOnlySpan<byte> NewLineBytes => "\n"u8;
private static readonly JsonSerializerOptions Options = new()
Expand All @@ -28,11 +28,13 @@ public class AzureBlobAuditLogStorage : IAuditLogStorage
private readonly AzureBlobAuditLogStorageConfiguration config;

public AzureBlobAuditLogStorage(
ILogger<AzureBlobAuditLogStorage> logger,
BlobServiceClient blobClient,
TableServiceClient tableClient,
AzureBlobAuditLogStorageConfiguration config
)
{
this.logger = logger;
this.blobClient = blobClient;
this.tableClient = tableClient;
this.config = config;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Net;
using System.Net.Http.Headers;
using IdentityModel.Client;
using LeanCode.Logging;

namespace LeanCode.ClientCredentialsHandler;

Expand All @@ -9,7 +10,7 @@ public class ClientCredentialsHandler : DelegatingHandler
{
private static readonly TimeSpan LockTimeout = TimeSpan.FromSeconds(5);

private readonly Serilog.ILogger logger = Serilog.Log.ForContext<ClientCredentialsHandler>();
private readonly ILogger<ClientCredentialsHandler> logger;

private readonly string tokenEndpoint;
private readonly ClientCredentialsConfiguration config;
Expand Down Expand Up @@ -41,9 +42,10 @@ private string? AccessToken
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("?", "CA2000", Justification = "Disposed by parent class.")]
public ClientCredentialsHandler(ClientCredentialsConfiguration config)
public ClientCredentialsHandler(ILogger<ClientCredentialsHandler> logger, ClientCredentialsConfiguration config)
: base(new HttpClientHandler())
{
this.logger = logger;
this.config = config;

httpClient = new HttpClient();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="Serilog" />
<PackageReference Include="IdentityModel" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../../Helpers/LeanCode.UrlHelper/LeanCode.UrlHelper.csproj" />
<ProjectReference Include="../../Infrastructure/LeanCode.Logging/LeanCode.Logging.csproj" />
</ItemGroup>

</Project>
5 changes: 4 additions & 1 deletion src/Infrastructure/LeanCode.Firebase.FCM/FCMClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@
using System.Globalization;
using FirebaseAdmin.Messaging;
using LeanCode.Localization.StringLocalizers;
using LeanCode.Logging;

namespace LeanCode.Firebase.FCM;

public class FCMClient<TUserId>
where TUserId : IEquatable<TUserId>
{
private readonly Serilog.ILogger logger = Serilog.Log.ForContext<FCMClient<TUserId>>();
private readonly ILogger<FCMClient<TUserId>> logger;

private readonly FirebaseMessaging messaging;
private readonly IPushNotificationTokenStore<TUserId> tokenStore;
private readonly IStringLocalizer stringLocalizer;

public FCMClient(
ILogger<FCMClient<TUserId>> logger,
FirebaseMessaging messaging,
IPushNotificationTokenStore<TUserId> tokenStore,
IStringLocalizer stringLocalizer
)
{
this.logger = logger;
this.messaging = messaging;
this.tokenStore = tokenStore;
this.stringLocalizer = stringLocalizer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

<ItemGroup>
<ProjectReference Include="../../Domain/LeanCode.TimeProvider/LeanCode.TimeProvider.csproj" />
<ProjectReference Include="../../Infrastructure/LeanCode.Logging/LeanCode.Logging.csproj" />
<ProjectReference Include="../LeanCode.Firebase/LeanCode.Firebase.csproj" />
<ProjectReference Include="../LeanCode.Localization/LeanCode.Localization.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog" />
<PackageReference Include="Microsoft.EntityFrameworkCore" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" />
</ItemGroup>
Expand Down
Loading