以编程方式跟踪UWP应用中的SignalR数据使用情况

时间:2017-10-25 22:38:26

标签: uwp signalr signalr.client

我有一个UWP应用程序连接到服务器以使用SignalR发送和接收数据。

我想跟踪我的连接使用的数据量(每天)。

从应用程序跟踪所有互联网使用情况也是可以接受的,因此通用技术而不是专门针对SignalR也是有用的。

有没有人有任何建议?我已着手实施自定义Microsoft.AspNet.SignalR.Client.Http.IHttpClient以及网络跟踪(https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-configure-network-tracing - 在UWP上不可用?)

@Ipsit Gaur的这项技术运作良好,我不得不稍微修改它

var internetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (internetConnectionProfile != null)
{
    var networkUsage = await internetConnectionProfile.GetNetworkUsageAsync(DateTimeOffset.UtcNow.Date, DateTimeOffset.UtcNow, DataUsageGranularity.PerDay, new NetworkUsageStates()).AsTask();
    totalBytesSentToday = networkUsage.Aggregate(0ul, (memo, nu) => memo + nu.BytesSent);
    totalBytesReceivedToday = networkUsage.Aggregate(0ul, (memo, nu) => memo + nu.BytesReceived);
}

1 个答案:

答案 0 :(得分:1)

这对你有用 -

string localDataUsage = string.Empty;
    DateTime CurrTime = DateTime.Now;
    TimeSpan TimeDiff = new TimeSpan(1, 0, 0);
    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

if (InternetConnectionProfile == null)
{
    LocalUsageData.Text = "Not connected to Internet" + "\n";
}
else
{
    var LocalUsage = InternetConnectionProfile.GetNetworkUsageAsync(CurrTime.Subtract(TimeDiff), CurrTime);

    localDataUsage = "Local Data Usage:\n";
    localDataUsage += " Bytes Sent     : " + LocalUsage.BytesSent + "\n";
    localDataUsage += " Bytes Received : " + LocalUsage.BytesReceived + "\n";
    LocalUsageData.Text = localDataUsage;
}