Akka.Net integrate with Microsoft.Extensions
Microsoft.Extensions.DependencyInjection Dependency Injection (DI) support for Akka.NET
// Create and build your container
var builder = new ServiceCollection();
builder.AddTransient<IWorkerService, WorkerService>();
builder.AddTransient<TypedWorker>();
var serviceProvider = builder.BuildServiceProvider();
// Create the ActorSystem and Dependency Resolver
var system = ActorSystem.Create("MySystem");
system.UseServiceProvider(serviceProvider);
When creating actorRefs straight off your ActorSystem instance, you can use the DI() Extension.
// Create the Props using the DI extension on your ActorSystem instance
var workerServiceRef = system.ActorOf(system.DI().Props<IWorkerService>(), "WorkerService");
var worker1Ref = system.ActorOf(system.DI().Props<TypedWorker>(), "Worker1");
var worker2Ref = system.ActorOf(system.DI().Props<TypedWorker>(), "Worker2");
Microsoft.Extensions.Logging logging adapter for Akka.NET
You'll need to set the static property LoggingLogger.LoggerFactory
and also specify to use the logger in the config when creating the system, for example like this:
var builder = new ServiceCollection()
.AddLogging(logging => logging.AddConsole().AddDebug());
var serviceProvider = builder.BuildServiceProvider();
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
LoggingLogger.LoggerFactory = loggerFactory;
var system = ActorSystem.Create("my-test-system", "akka { loglevel=INFO, loggers=[\"Akka.Logger.Extensions.Logging.LoggingLogger, Akka.Logger.Extensions.Logging\"]}");
To log inside an actor:
using Akka.Logger.Extensions.Logging;
var log = Context.GetLogger();
...
log.Info("The value is {Counter}", counter);