Akka.NET 记录器未将日志发送到 Application Insights

Akka.NET logger not sending logs to Application Insights

我在 Akka.NET 中将 Application Insights 设置为日志记录提供程序,并设置 Akka.Monitoring 来记录各种自定义指标(例如计算接收到的消息和计时处理持续时间)。这很好用。

但是我使用 Akka.NET 的记录器生成的日志不会发送到 Application Insights:

var logger = Logging.GetLogger(Context);
logger.Info($"Please send me to Azure!");

我希望在 Application Insights 的“跟踪”部分中看到它以及我的其他 .NET Core 日志项。然而,这些日志确实出现在我的运行时输出中,我假设它是标准输出。

Application Insights 使用 Akka.Monitoring.ApplicationInsights 配置如下:

ActorMonitoringExtension.RegisterMonitor(system, new ActorAppInsightsMonitor(instrumentationKey));

解决方案:

感谢 Peter 的回答,我不得不为此实现一个自定义记录器:

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
            });
        }
    }
}

快速扫描显示对 App Insights 的唯一调用是 TrackMetrics 个调用。要发送 logger.Info($"Please send me to Azure!"); 之类的日志消息,需要调用 TrackTrace。所以我想这个库只对跟踪指标有用,对消息没有用。包裹描述如下:

进一步证实了这一点

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

无论如何,documentation 并不表示有可用的 Application Insights 记录器。