azure - Akka.NET 记录器不将日志发送到 Application Insights

标签 azure akka azure-application-insights akka.net akka-monitoring

我已将 Application Insights 设置为 Akka.NET 中的日志记录提供程序,并设置 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 记录器。

关于azure - Akka.NET 记录器不将日志发送到 Application Insights,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59873016/

相关文章:

scala - Akka context.actorOf 不工作

c# - Azure 快照调试和 Azure Functions

azure - 如何获取Azure DevOps服务连接ID

azure - 获取 #error=unsupported_response_type&error_description=AADSTS70005 : with token request

azure - 如何为 Azure 上的网站预览选择 Windows Server 2012

java - Akka 循环赛池

java - 使用 Java AzureResourceManager 进行异步模板部署

scala - 如何将 Akka Streams SourceQueue 与 PlayFramework 一起使用

Azure:应用程序洞察。是否需要 ai.x.xx.x-buildxxxxx.js 文件?

azure - 在 Azure 应用程序见解中,给定时间段内的 session 数怎么可能多于用户数?