Skip to content

Commit

Permalink
MultiTenancy Logger Scope (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
fraliv13 authored Jan 14, 2025
1 parent 2e91f11 commit 577beb0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/Messaging/NBB.Messaging.MultiTenancy/TenantMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ public async Task Invoke(MessagingContext context, CancellationToken cancellatio

Activity.Current?.SetTag(TracingTags.TenantId, tenant.TenantId);

await next();
using (logger.BeginScope(new TenantLogScope(tenantContextAccessor.TenantContext)))
{
await next();
}
}

private async Task<Tenant> LoadTenant(CancellationToken cancellationToken)
Expand Down
61 changes: 61 additions & 0 deletions src/MultiTenancy/NBB.MultiTenancy.Abstractions/TenantLogScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) TotalSoft.
// This source code is licensed under the MIT license.

using NBB.MultiTenancy.Abstractions.Context;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;

namespace NBB.MultiTenancy.Abstractions
{
public sealed class TenantLogScope(TenantContext tenantContext) : IReadOnlyList<KeyValuePair<string, object>>
{
private string? _cachedToString;

Check warning on line 14 in src/MultiTenancy/NBB.MultiTenancy.Abstractions/TenantLogScope.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in src/MultiTenancy/NBB.MultiTenancy.Abstractions/TenantLogScope.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in src/MultiTenancy/NBB.MultiTenancy.Abstractions/TenantLogScope.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in src/MultiTenancy/NBB.MultiTenancy.Abstractions/TenantLogScope.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in src/MultiTenancy/NBB.MultiTenancy.Abstractions/TenantLogScope.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in src/MultiTenancy/NBB.MultiTenancy.Abstractions/TenantLogScope.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

public Guid TenantId => tenantContext?.GetTenantId() ?? throw new ArgumentNullException(nameof(tenantContext));
public string TenantCode => tenantContext?.GetTenantCode() ?? throw new ArgumentNullException(nameof(tenantContext));
public int Count => 2;

public KeyValuePair<string, object> this[int index]
{
get
{
if (index == 0)
{
return new KeyValuePair<string, object>(nameof(TenantId), TenantId);
}
else if (index == 1)
{
return new KeyValuePair<string, object>(nameof(TenantCode), TenantCode);
}

throw new ArgumentOutOfRangeException(nameof(index));
}
}

public override string ToString()
{
_cachedToString ??= string.Format(
CultureInfo.InvariantCulture,
"TenantId:{0} TenantCode:{1}",
TenantId,
TenantCode);

return _cachedToString;
}

public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
for (var i = 0; i < Count; ++i)
{
yield return this[i];
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
19 changes: 7 additions & 12 deletions src/MultiTenancy/NBB.MultiTenancy.AspNet/TenantMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@
using NBB.MultiTenancy.Abstractions.Repositories;
using NBB.MultiTenancy.Identification.Services;
using NBB.MultiTenancy.Abstractions;
using Microsoft.Extensions.Logging;

namespace NBB.MultiTenancy.AspNet
{
public class TenantMiddleware
public class TenantMiddleware(RequestDelegate next, ILogger<TenantMiddleware> logger)
{
private readonly RequestDelegate _next;

public TenantMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context, ITenantIdentificationService tenantIdentificationService,
ITenantContextAccessor tenantContextAccessor, ITenantRepository tenantRepository, IOptions<TenancyHostingOptions> tenancyOptions)
{
Expand All @@ -34,7 +28,7 @@ public async Task Invoke(HttpContext context, ITenantIdentificationService tenan
if (tenancyOptions.Value.TenancyType == TenancyType.MonoTenant)
{
tenantContextAccessor.TenantContext = new TenantContext(Tenant.Default);
await _next(context);
await next(context);
return;
}

Expand All @@ -44,9 +38,10 @@ public async Task Invoke(HttpContext context, ITenantIdentificationService tenan

tenantContextAccessor.TenantContext = new TenantContext(tenant);

await _next(context);
using (logger.BeginScope(new TenantLogScope(tenantContextAccessor.TenantContext)))
{
await next(context);
}
}


}
}

0 comments on commit 577beb0

Please sign in to comment.