Akka.NET无法识别我的自定义记录器,默认为BusLogger

时间:2018-06-26 22:34:53

标签: c# logging .net-core akka.net hocon

我正在学习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,我看不到使用配置中设置的记录器的任何地方。

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

Program.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}");
        }
    }
}

app.config:

<?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>

1 个答案:

答案 0 :(得分: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>