using System;
using System.Collections.Generic;
using System.Linq;
using ComputeSharp.SwapChain.Core.Services;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
#nullable enable
namespace ComputeSharp.SwapChain.Uwp.Services;
///
/// An implementation using AppCenter.
///
public sealed class AppCenterService : IAnalyticsService
{
///
/// The maximum length for any property name and value
///
/// It's 125, but one character is reserved for the leading '|' to indicate trimming.
private const int PropertyStringMaxLength = 124;
///
public AppCenterService(string secret)
{
AppCenter.Start(secret, typeof(Crashes), typeof(Analytics));
}
///
public void Log(string title, params (string Property, object? Value)[]? data)
{
Analytics.TrackEvent(title, GetProperties(data));
}
///
public void Log(Exception exception, params (string Property, object? Value)[]? data)
{
Crashes.TrackError(exception, GetProperties(data));
}
///
/// Gets the additional logging properties from the input data.
///
/// The optional event properties.
/// The additional logging properties to track.
private static IDictionary? GetProperties((string Property, object? Value)[]? data)
{
return
data?.ToDictionary(
pair => pair.Property,
pair =>
{
string text = (pair.Value ?? "").ToString();
return text.Length <= PropertyStringMaxLength
? text
: $"|{text.Substring(text.Length - PropertyStringMaxLength)}";
});
}
}