Skip to content

Latest commit

 

History

History
63 lines (45 loc) · 3.39 KB

README.md

File metadata and controls

63 lines (45 loc) · 3.39 KB

Build Status Lines of Code Coverage Reliability Rating Security Rating

Akka.DI.Extensions.DependencyInjection

Microsoft.Extensions.DependencyInjection Dependency Injection (DI) support for Akka.NET

NuGet

// 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");

Akka.Logger.Extensions.Logging

Microsoft.Extensions.Logging logging adapter for Akka.NET

NuGet

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);