2

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

我检查了ActorSystemImpl 类的 Akka.NET 源代码。在第 441 行,记录器设置为 BusLogging,我看不到任何使用配置中设置的记录器的地方。

我创建了一个针对 .NET core 2.0 的非常简单的 Akka.NET 项目来演示该问题。完整的源代码如下。我在这里想念什么?为什么我不能按照教程描述的那样连接我的自定义记录器?

程序.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>
4

1 回答 1

1

您的问题是您的 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>
于 2018-06-27T13:46:03.770 回答