Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Akka.Hosting example #230

Merged
merged 3 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Akka.Logger.Serilog.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{489D8D37
build.sh = build.sh
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{28CDBE4C-053C-4CD4-B5D2-4DF529922916}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Akka.Hosting.LoggingDemo", "src\Examples\Akka.Hosting.LoggingDemo\Akka.Hosting.LoggingDemo.csproj", "{1E83DF8F-408B-48DB-8896-B81AD742E7BB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -35,11 +39,18 @@ Global
{CAE7CA7C-0D0C-4FDA-BDE9-BE16A27343EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CAE7CA7C-0D0C-4FDA-BDE9-BE16A27343EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CAE7CA7C-0D0C-4FDA-BDE9-BE16A27343EF}.Release|Any CPU.Build.0 = Release|Any CPU
{1E83DF8F-408B-48DB-8896-B81AD742E7BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E83DF8F-408B-48DB-8896-B81AD742E7BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E83DF8F-408B-48DB-8896-B81AD742E7BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E83DF8F-408B-48DB-8896-B81AD742E7BB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6F6A6366-6F90-4FC6-8E46-861B3CB6C12B}
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{1E83DF8F-408B-48DB-8896-B81AD742E7BB} = {28CDBE4C-053C-4CD4-B5D2-4DF529922916}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Akka.Cluster.Hosting" Version="1.5.6" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Akka.Logger.Serilog\Akka.Logger.Serilog.csproj" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions src/Examples/Akka.Hosting.LoggingDemo/Echo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Akka.Hosting.LoggingDemo;

public struct Echo{}
72 changes: 72 additions & 0 deletions src/Examples/Akka.Hosting.LoggingDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Akka.Hosting;
using Akka.Actor;
using Akka.Actor.Dsl;
using Akka.Cluster.Hosting;
using Akka.Event;
using Akka.Hosting.Logging;
using Akka.Hosting.LoggingDemo;
using Akka.Logger.Serilog;
using Akka.Remote.Hosting;
using Serilog;
using Serilog.Core.Enrichers;
using Serilog.Formatting.Json;
using LogLevel = Akka.Event.LogLevel;

Serilog.Log.Logger = new LoggerConfiguration()
.WriteTo.Console(outputTemplate:"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.WriteTo.File(new JsonFormatter(), "output.json")
.MinimumLevel.Debug()
.CreateLogger();

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAkka("MyActorSystem", (configurationBuilder, serviceProvider) =>
{
configurationBuilder
.ConfigureLoggers(setup =>
{
// This sets the minimum log level
setup.LogLevel = LogLevel.InfoLevel;

// Clear all loggers
setup.ClearLoggers();

// Add serilog logger
setup.AddLogger<SerilogLogger>();
setup.LogMessageFormatter = typeof(SerilogLogMessageFormatter);
})
.WithRemoting("localhost", 8110)
.WithClustering(new ClusterOptions(){ Roles = new[]{ "myRole" },
SeedNodes = new[]{ "akka.tcp://MyActorSystem@localhost:8110" }})
.WithActors((system, registry) =>
{
var echo = system.ActorOf(act =>
{
var counter = 0;
act.ReceiveAny((o, context) =>
{
counter++;
context.GetLogger().Info("Actor received {ID}", o, new PropertyEnricher(
"custom-property",
new
{
name = "Custom",
data = counter
}));
context.Sender.Tell($"{context.Self} rcv {o}");
});
}, "echo");
registry.TryRegister<Echo>(echo); // register for DI
});
});

var app = builder.Build();

app.MapGet("/", async (context) =>
{
var echo = context.RequestServices.GetRequiredService<ActorRegistry>().Get<Echo>();
var body = await echo.Ask<string>(context.TraceIdentifier, context.RequestAborted).ConfigureAwait(false);
await context.Response.WriteAsync(body);
});

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
11 changes: 11 additions & 0 deletions src/Examples/Akka.Hosting.LoggingDemo/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"Akka": "Debug"
}
},
"AllowedHosts": "*"
}