我花了两天的大部分时间来解决以下问题,但我已经没有想法了。希望有类似经历的人可以帮助我。
我有一个具有共享后端的 .NET 8.0 iOS、Windows 和 Android 应用程序。异常日志记录到应用程序洞察对于 iOS 和 Windows 来说工作得很好,但它只适用于我应用程序的 DEBUG 配置中的 Android。
复制
在应用程序启动期间实例化遥测客户端:
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
var telemetryConnection = config["ApplicationInsightsConnection"];
telemetryConfiguration.ConnectionString = telemetryConnection;
var telemetryClient = new TelemetryClient(telemetryConfiguration);
Mvx.IoCProvider.RegisterSingleton(telemetryClient);
将事件或异常写入 Application Insights:
/// <inheritdoc />
public void LogException(Exception exception, string? message, IDictionary<string, string> properties)
{
var telemetryClient = Mvx.IoCProvider?.Resolve<TelemetryClient>();
// Both Don't work
telemetryClient?.TrackException(new ExceptionTelemetry(new Exception("Test Thomas Release Config")));
telemetryClient?.TrackEvent("Test Thomas Release Config");
}
预期行为
在 Android 上,在发布模式下编译我的应用程序后,我希望使用 telemetryClient.Track 发送的异常在应用程序洞察中可见。
相关包、工具和运行时版本
应用洞察版本2.22.0 平台:.Net 8 应用程序、Android(无 Maui)。
其他背景
proguard-rules.pro:
-keep class com.microsoft.** { *; }
-keep class io.opentelemetry.** { *; }
-keep class com.azure.** { *; }
-keep class com.microsoft.applicationinsights.** { *; }
在这里,我已经为应用程序安装了以下依赖项。
发布模式配置与调试模式配置不同。检查您是否在应用程序关闭或崩溃之前刷新遥测数据。这在发布模式下尤其重要,其中优化可能会导致应用程序更积极地终止。
App.xaml.cs:
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
namespace MauiApp1
{
public partial class App : Application
{
private static TelemetryClient _telemetryClient;
public App()
{
InitializeComponent();
// Initialize Application Insights
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
telemetryConfiguration.ConnectionString = "InstrumentationKey=1dc7bxx8-xxxx-487e-xxxx-af89xxxxxxa1;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/;ApplicationId=5fxxxxx5-xxxx41b6-axxx-9xxxx4xxxx1e"; // Replace with your actual Application Insights connection string
_telemetryClient = new TelemetryClient(telemetryConfiguration);
MainPage = new AppShell();
// Log an exception on app start for testing
LogException(new Exception("Test exception on app start"));
}
public static void LogException(Exception exception)
{
_telemetryClient?.TrackException(exception);
_telemetryClient?.Flush(); // Ensure telemetry is sent immediately
}
public static void LogEvent(string eventName)
{
_telemetryClient?.TrackEvent(eventName);
_telemetryClient?.Flush(); // Ensure telemetry is sent immediately
}
}
}
MainPage.xaml.cs:
using System;
using Microsoft.ApplicationInsights;
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
int count = 0;
public MainPage()
{
InitializeComponent();
}
private void OnCounterClicked(object sender, EventArgs e)
{
count++;
if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";
SemanticScreenReader.Announce(CounterBtn.Text);
// Log a custom event for button clicks
App.LogEvent("ButtonClicked");
}
}
}
日志跟踪: