Akka.NET not recognising my custom logger and defa

2019-07-09 05:43发布

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.

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.

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>

1条回答
孤傲高冷的网名
2楼-- · 2019-07-09 06:13

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>
查看更多
登录 后发表回答