Skip to content

Commit 64159fc

Browse files
committed
Create LoggerBeginScopeWithAuditFrame class and use it to add logger.BeginScope calls for Chain AuditedMembers and AggregateHandler AggregateType audited members
1 parent a02bd42 commit 64159fc

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/Persistence/Wolverine.Marten/AggregateHandlerAttribute.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Marten.Schema;
1212
using Wolverine.Attributes;
1313
using Wolverine.Configuration;
14+
using Wolverine.Logging;
1415
using Wolverine.Marten.Codegen;
1516
using Wolverine.Marten.Publishing;
1617
using Wolverine.Runtime.Handlers;
@@ -76,8 +77,6 @@ public override void Modify(IChain chain, GenerationRules rules, IContainer cont
7677
AggregateIdMember = DetermineAggregateIdMember(AggregateType, CommandType);
7778
VersionMember = DetermineVersionMember(CommandType);
7879

79-
80-
8180
var sessionCreator = MethodCall.For<OutboxedSessionFactory>(x => x.OpenSession(null!));
8281
chain.Middleware.Add(sessionCreator);
8382

@@ -89,7 +88,10 @@ public override void Modify(IChain chain, GenerationRules rules, IContainer cont
8988
chain.Middleware.Add(new MissingAggregateCheckFrame(AggregateType, CommandType, AggregateIdMember,
9089
loader.ReturnVariable!));
9190
}
92-
91+
92+
chain.Middleware.Add(new LoggerBeginScopeWithAuditFrame(
93+
AuditedMember.GetAllFromType(AggregateType).ToList(),
94+
new MemberAccessVariable(loader.ReturnVariable!, typeof(IEventStream<>).MakeGenericType(AggregateType).GetProperty("Aggregate")!)));
9395
// Use the active document session as an IQuerySession instead of creating a new one
9496
firstCall.TrySetArgument(new Variable(typeof(IQuerySession), sessionCreator.ReturnVariable!.Usage));
9597

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using JasperFx.CodeGeneration;
2+
using JasperFx.CodeGeneration.Frames;
3+
using JasperFx.CodeGeneration.Model;
4+
using JasperFx.Core.Reflection;
5+
using Microsoft.Extensions.Logging;
6+
using Wolverine.Configuration;
7+
8+
namespace Wolverine.Logging;
9+
10+
internal class LoggerBeginScopeWithAuditFrame : SyncFrame
11+
{
12+
private readonly IReadOnlyList<AuditedMember> _members;
13+
private readonly Type? _inputType;
14+
private Variable? _logger;
15+
private Variable? _withAudit;
16+
17+
public LoggerBeginScopeWithAuditFrame(IChain chain)
18+
{
19+
_members = chain.AuditedMembers.AsReadOnly();
20+
_inputType = chain.InputType()!;
21+
}
22+
public LoggerBeginScopeWithAuditFrame(IReadOnlyList<AuditedMember> members, Variable variable)
23+
{
24+
_members = members;
25+
_withAudit = variable;
26+
}
27+
28+
public override IEnumerable<Variable> FindVariables(IMethodVariables chain)
29+
{
30+
_logger = chain.FindVariable(typeof(ILogger));
31+
yield return _logger;
32+
33+
if (_inputType is null) yield break;
34+
_withAudit = chain.FindVariable(_inputType);
35+
yield return _withAudit;
36+
}
37+
38+
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
39+
{
40+
if (_members.Count == 0) return;
41+
writer.WriteComment("Adding audited members to log context");
42+
writer.Write(
43+
$"using var disposable_{Guid.NewGuid().ToString().Replace("-", "_")} = {_logger!.Usage}.{nameof(ILogger.BeginScope)}"
44+
+ $"(new {typeof(Dictionary<string, object>).FullNameInCode()}(){{{string.Join(", ", _members.Select(member => $"{{\"{member.MemberName}\", {_withAudit!.Usage}.{member.Member.Name}}}"))}}});");
45+
46+
Next?.GenerateCode(method, writer);
47+
}
48+
49+
}

src/Wolverine/Runtime/Handlers/HandlerChain.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ internal virtual List<Frame> DetermineFrames(GenerationRules rules, IContainer c
303303

304304
if (AuditedMembers.Any())
305305
{
306-
Middleware.Insert(0, new AuditToActivityFrame(this));
306+
Middleware.Insert(0, new LoggerBeginScopeWithAuditFrame(this));
307+
Middleware.Insert(1, new AuditToActivityFrame(this));
307308
}
308309

309310
Middleware.Insert(0, messageVariable.Creator!);

0 commit comments

Comments
 (0)