-
-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (62 loc) · 2.19 KB
/
Program.cs
File metadata and controls
74 lines (62 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// -----------------------------------------------------------------------
// <copyright file="Program.cs" company="Asynkron AB">
// Copyright (C) 2015-2024 Asynkron AB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Threading;
using System.Threading.Tasks;
using Proto;
namespace ContextDecorators;
public class LoggingRootDecorator : RootContextDecorator
{
public LoggingRootDecorator(IRootContext context) : base(context)
{
}
public override async Task<T> RequestAsync<T>(PID target, object message, CancellationToken ct)
{
Console.WriteLine("Enter RequestAsync");
var res = await base.RequestAsync<T>(target, message, ct);
Console.WriteLine("Exit RequestAsync");
return res;
}
}
public class LoggingDecorator : ActorContextDecorator
{
private readonly string _loggerName;
public LoggingDecorator(IContext context, string loggerName) : base(context)
{
_loggerName = loggerName;
}
//we are just logging this single method
public override void Respond(object message)
{
Console.WriteLine($"{_loggerName} : Enter Respond");
base.Respond(message);
Console.WriteLine($"{_loggerName} : Exit Respond");
}
}
internal class Program
{
private static void Main(string[] args)
{
var context = new LoggingRootDecorator(new RootContext(new ActorSystem()));
var props = Props.FromFunc(ctx =>
{
if (ctx.Message is string str)
{
Console.WriteLine("Inside Actor: " + str);
ctx.Respond("Yo!");
}
return Task.CompletedTask;
}
)
.WithContextDecorator(c => new LoggingDecorator(c, "logger1"))
.WithContextDecorator(c => new LoggingDecorator(c, "logger2"))
;
var pid = context.Spawn(props);
var res = context.RequestAsync<string>(pid, "Hello").Result;
Console.WriteLine("Got result " + res);
Console.ReadLine();
}
}