Akka.NET无法识别我的自定义记录器,默认为BusLogger [英] Akka.NET not recognising my custom logger and defaulting to BusLogger

查看:126
本文介绍了Akka.NET无法识别我的自定义记录器,默认为BusLogger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am learning Akka.NET. I am trying to create a custom logger. I followed a tutorial blog post here. I have been unable to get Akka to wire up my custom logger. Apparently the line var sys = ActorSystem.Create("AkkaCustomLoggingActorSystem"); reads in the Akka hocon and configures the logging as per the settings. When I check the value of sys after the actor system is created, I can see the configuration string saved but the logger is of type BusLogger instead of my custom logger.

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

I checked the Akka.NET source for the ActorSystemImpl class. At line 441 the logger is set to BusLogging and I cannot see anywhere that the logger set in the config is used.

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

I have a created an extremely simple Akka.NET project targeting .NET core 2.0 that demonstrates the issue. The complete source is below. What am I missing here? Why can I not wire up my custom logger as the tutorial describes?

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>

推荐答案

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

Your issue is that your HOCON is missing the akka namespace - it should read:

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

这篇关于Akka.NET无法识别我的自定义记录器,默认为BusLogger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆