Elias Ghali
Elias Ghali

Reputation: 853

Akka.Net logging to file

I am trying to follow this tutorial:

to add logging to my Akka.Net application but the log file is not being created. Below is my App.config file:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="akka" type="Akka.Configuration.Hocon.AkkaConfigurationSection, Akka" />
      </configSections>
      <akka>
        <hocon>
          <![CDATA[ 
            akka 
              { 
                loglevel = INFO
                loggers = ["Akka.Logger.NLog.NLogLogger, Akka.Logger.NLog"]
              }
          ]]>
        </hocon>
      </akka>
    </configuration>

and this is my NLog.config file:

    <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
      <targets>
        <target name="file" xsi:type="File" fileName="C:\Users\User\Documents\logs\test.log" />
      </targets>
    
      <rules>
        <logger name="*" minlevel="Info" writeTo="file" />
      </rules>
    
    </nlog>

Program.cs

static void Main(string[] args)
        {            
            ActorSystem actorSystem = ActorSystem.Create("IMSIFilteringActorSystem");
            
            IActorRef fileSearcherActor = actorSystem.ActorOf(Props.Create(() => new FileSearcherActor()), "fileSearcherActor");

            ILoggingAdapter logger = Logging.GetLogger(actorSystem, actorSystem, null);

            actorSystem.Scheduler.ScheduleTellRepeatedly(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(5), fileSearcherActor, engineParams, ActorRefs.NoSender);

            logger.Info("Sending messages from logging");
}

Any help is appreciated, Thank you

Upvotes: 0

Views: 612

Answers (1)

nikmaz
nikmaz

Reputation: 21

I think you miss the NLog configuration. Try something like this:

var config = new LoggingConfiguration();
var fileTarget = new FileTarget("fileTargetName")
{
   FileName = "Absolute path to your log file.",
   // The layout I once composed. Use yours or remove this property initialization at all.
   Layout = @"[${level}][${longdate}][${stacktrace:format=Flat}]${literal:text=[Exception\: :when=length('${exception}')>0}${exception}${literal:text=]:when=length('${exception}')>0} <${message}>",
};
config.AddTarget(fileTarget);
config.AddRuleForAllLevels(fileTarget);
LogManager.Configuration = config;

To view all log entries (including those generated by Akka.NET itself), use the following configuration in your App.config:

akka
{
   loggers = ["Akka.Logger.NLog.NLogLogger, Akka.Logger.NLog"]
   loglevel = debug
   log-config-on-start = on
   actor
   {
      debug
      {
         receive = on      # log any received message
         autoreceive = on  # log automatically received messages, e.g. PoisonPill
         lifecycle = on    # log actor lifecycle changes
         event-stream = on # log subscription changes for Akka.NET event stream
         unhandled = on    # log unhandled messages sent to actors
      }
   }
}

Upvotes: 2

Related Questions