我正在学习akka.net。我正在尝试创建自定义记录器。我跟着一个
tutorial blog post here
.我无法让Akka连接我的定制日志。显然,这条线
var sys = ActorSystem.Create("AkkaCustomLoggingActorSystem");
读取akka hocon并根据设置配置日志。当我检查
sys
在创建actor系统之后,我可以看到保存的配置字符串,但记录器的类型为
BusLogger
而不是我的自定义记录器。
我查过了
Akka.NET source for the ActorSystemImpl class
.在第441行,logger设置为buslogging,在配置中的任何地方都看不到logger设置。
我创建了一个非常简单的akka.net项目,目标是.NET核心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();
}
}
}
自定义记录器.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>