I've set up Application Insights as a logging provider in Akka.NET and set up Akka.Monitoring
to record various custom metrics (such as counting messages received and timing processing duration). This works great.
But the logs I generate using Akka.NET's logger do not get sent to Application Insights:
var logger = Logging.GetLogger(Context);
logger.Info($"Please send me to Azure!");
I would expect to see it in the Traces section on Application Insights along with my other .NET Core log items. These logs, however, do appear in my runtime output, which I assume is stdout.
Application Insights is configured using Akka.Monitoring.ApplicationInsights
like so:
ActorMonitoringExtension.RegisterMonitor(system, new ActorAppInsightsMonitor(instrumentationKey));
SOLUTION:
Thanks to Peter's answer, I had to implement a custom logger for this:
public class ApplicationInsightsLogger
: ReceiveActor
{
private readonly TelemetryClient _telemetry = new TelemetryClient();
private readonly IDictionary<LogLevel, SeverityLevel> _logLevelMap = new Dictionary<LogLevel, SeverityLevel>()
{
{ LogLevel.DebugLevel, SeverityLevel.Verbose },
{ LogLevel.InfoLevel, SeverityLevel.Information },
{ LogLevel.WarningLevel, SeverityLevel.Warning },
{ LogLevel.ErrorLevel, SeverityLevel.Error },
};
public ApplicationInsightsLogger()
{
Receive<LogEvent>(message => this.Log(message.LogLevel(), message));
Receive<InitializeLogger>(_ => Sender.Tell(new LoggerInitialized()));
}
private void Log(LogLevel level, LogEvent item)
{
if (!_logLevelMap.ContainsKey(level))
{
throw new InvalidOperationException($"{level} log level isn't handled.");
}
SeverityLevel severity = _logLevelMap[level];
_telemetry.TrackTrace(new TraceTelemetry()
{
Message = item.Message.ToString(),
SeverityLevel = severity,
Timestamp = item.Timestamp,
Properties = { { "Source", item.LogSource } }
});
if (item is Error)
{
_telemetry.TrackException(new ExceptionTelemetry()
{
Message = item.Message.ToString(),
SeverityLevel = severity,
Timestamp = item.Timestamp,
Properties = { { "Source", item.LogSource } },
Exception = (item as Error).Cause
});
}
}
}
A quick scan reveals that the only calls to App Insights are TrackMetrics
calls. To send log messages like logger.Info($"Please send me to Azure!");
one would expect a call to TrackTrace
. So I suppose the library is only useful to track metrics, not messages. This is further confirmed by the fact that the package description reads:
Akka.Monitoring is an ActorSystem extension for Akka.NET that exposes a pluggable layer for reporting performance metrics from actors back to a monitoring system suc ....
Anyway, the documentation does not indicate there is an Application Insights logger available.