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

查看:17
本文介绍了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天全站免登陆