Skip to content

Commit 0716d34

Browse files
authored
Added user id to logging (#101)
1 parent a787865 commit 0716d34

4 files changed

Lines changed: 34 additions & 5 deletions

File tree

source/EasyWay/Internals/Commands/Commands/CommandExecutorLoggerDecorator.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,18 @@ public async Task<CommandResult> Execute<TModule, TCommand>(TCommand command, Ca
2323
{
2424
var logger = _serviceProvider.GetRequiredService<EasyWayLogger<TModule>>();
2525

26+
var userContext = _serviceProvider.GetRequiredService<IUserContext>();
27+
2628
//TODO begin scope (correlation Id, userId)
2729

28-
logger.Executing(command);
30+
if (userContext.UserId is not null)
31+
{
32+
logger.ExecutingByUser(command, userContext.UserId);
33+
}
34+
else
35+
{
36+
logger.Executing(command);
37+
}
2938

3039
try
3140
{

source/EasyWay/Internals/Commands/CommandsWithResult/CommandWithOperationResultExecutorLoggerDecorator.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,18 @@ public async Task<CommandResult<TOperationResult>> Command<TModule, TCommand, TO
2424
{
2525
var logger = _serviceProvider.GetRequiredService<EasyWayLogger<TModule>>();
2626

27+
var userContext = _serviceProvider.GetRequiredService<IUserContext>();
28+
2729
//TODO begin scope (correlation Id, userId)
2830

29-
logger.Executing(command);
31+
if (userContext.UserId is not null)
32+
{
33+
logger.ExecutingByUser(command, userContext.UserId);
34+
}
35+
else
36+
{
37+
logger.Executing(command);
38+
}
3039

3140
try
3241
{

source/EasyWay/Internals/Loggers/EasyWayLogger.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using EasyWay.Internals.BusinessRules;
2-
using Microsoft.Extensions.Logging;
1+
using Microsoft.Extensions.Logging;
32

43
namespace EasyWay.Internals.Queries.Loggers
54
{
@@ -13,6 +12,9 @@ internal sealed partial class EasyWayLogger<TModule>
1312
[LoggerMessage(0, LogLevel.Information, "Executing {@component}", SkipEnabledCheck = true)]
1413
public partial void Executing(object component);
1514

15+
[LoggerMessage(0, LogLevel.Information, "Executing {@component} by @userId", SkipEnabledCheck = true)]
16+
public partial void ExecutingByUser(object component, string userId);
17+
1618
[LoggerMessage(1, LogLevel.Information, "Successed", SkipEnabledCheck = true)]
1719
public partial void Successed();
1820

source/EasyWay/Internals/Queries/QueryExecutorLoggerDecorator.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@ public async Task<QueryResult<TReadModel>> Execute<TModule, TQuery, TReadModel>(
2525
{
2626
var logger = _serviceProvider.GetRequiredService<EasyWayLogger<TModule>>();
2727

28+
var userContext = _serviceProvider.GetRequiredService<IUserContext>();
29+
2830
//TODO begin scope (correlation Id, userId)
2931

30-
logger.Executing(query);
32+
if (userContext.UserId is not null)
33+
{
34+
logger.ExecutingByUser(query, userContext.UserId);
35+
}
36+
else
37+
{
38+
logger.Executing(query);
39+
}
3140

3241
try
3342
{

0 commit comments

Comments
 (0)