gpt4 book ai didi

c# - Akka.NET 无法识别我的自定义记录器并默认为 BusLogger

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:16 25 4
gpt4 key购买 nike

我正在学习 Akka.NET。我正在尝试创建自定义记录器。我关注了 tutorial blog post here .我一直无法让 Akka 连接我的自定义记录器。显然 var sys = ActorSystem.Create("AkkaCustomLoggingActorSystem"); 行读取 Akka hocon 并根据设置配置日志记录。当我在创建 actor 系统后检查 sys 的值时,我可以看到保存的配置字符串,但记录器的类型是 BusLogger 而不是我的自定义记录器。

我检查了 Akka.NET source for the ActorSystemImpl class .在第 441 行,记录器设置为 BusLogging,我在任何地方都看不到配置中设置的记录器被使用。

我创建了一个非常简单的 Akka.NET 项目,该项目以 .NET Core 2.0 为目标来演示该问题。完整的来源如下。我在这里错过了什么?为什么我不能像教程中描述的那样连接我的自定义记录器?

程序.cs:

using Akka.Actor;
using Akka.Event;
using System;

namespace AkkaCustomLogging
{
class Program
{
static void Main(string[] args)
{
var sys = ActorSystem.Create("AkkaCustomLoggingActorSystem");
var logger = Logging.GetLogger(sys, sys, null); // Always bus logger
Console.ReadLine();
}
}
}

CustomLogger.cs:

using Akka.Actor;
using Akka.Event;
using System;

namespace AkkaCustomLogging
{
public class CustomLogger : ReceiveActor
{
public CustomLogger()
{
Receive<Debug>(e => this.Log(LogLevel.DebugLevel, e.ToString()));
Receive<Info>(e => this.Log(LogLevel.InfoLevel, e.ToString()));
Receive<Warning>(e => this.Log(LogLevel.WarningLevel, e.ToString()));
Receive<Error>(e => this.Log(LogLevel.ErrorLevel, e.ToString()));
Receive<InitializeLogger>(_ => this.Init(Sender));
}

private void Init(IActorRef sender)
{
Console.WriteLine("Init");
sender.Tell(new LoggerInitialized());
}

private void Log(LogLevel level, string message)
{
Console.WriteLine($"Log {level} {message}");
}
}
}

应用程序配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
<section name="akka" type="Akka.Configuration.Hocon.AkkaConfigurationSection, Akka" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<akka>
<hocon>
<![CDATA[
loggers = [ "AkkaCustomLogging.CustomLogger, AkkaCustomLogging" ]
loglevel = warning
log-config-on-start = on
stdout-loglevel = off
actor {
debug {
receive = on
autoreceive = on
lifecycle = on
event-stream = on
unhandled = on
}
}
]]>
</hocon>
</akka>
</configuration>

最佳答案

您的问题是您的 HOCON 缺少 akka 命名空间 - 它应该显示为:

<akka>
<hocon>
<![CDATA[
akka {
loggers = [ "AkkaCustomLogging.CustomLogger, AkkaCustomLogging" ]
loglevel = warning
log-config-on-start = on
stdout-loglevel = off
actor {
debug {
receive = on
autoreceive = on
lifecycle = on
event-stream = on
unhandled = on
}
}
}
]]>
</hocon>
</akka>

关于c# - Akka.NET 无法识别我的自定义记录器并默认为 BusLogger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51052222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com