如何从 UWP 应用获取诊断统计信息
How to get diagnostic statistics from a UWP app
我们需要将我们拥有的应用程序升级到通用 Windows 应用程序 (UWP) 框架。该应用程序的主要目的是收集诊断统计信息并通过 rest 调用将它们发送回我们的服务器。
但是我找不到任何 api 可用于获取统计信息
之前我们是
PerformanceCounter _memoryCounter = new PerformanceCounter();
public SystemProperty GetPhysicalMemory()
{
string s = _QueryComputerSystem("totalphysicalmemory");
double totalphysicalmemory = Convert.ToDouble(s);
double d = _GetCounterValue(_memoryCounter, "Memory", "Available Bytes", null);
return new SystemProperty { PropertyName = "Physical Memory", Total = totalphysicalmemory, Used = totalphysicalmemory - d };
}
返回已用和可用内存的总字节数。我们还收集了网络和 cpu 统计数据。 None 其中与新框架兼容。我应该为这个功能寻找什么命名空间?或者我需要使用 Pinvoke () 之类的东西吗?
您可以使用 MemoryManager 获取设备的内存
https://msdn.microsoft.com/en-us/library/windows.system.memorymanager.aspx
还可以获取设备信息
https://msdn.microsoft.com/en-us/library/windows/apps/windows.system.profile.aspx
更新:这是获取当前设备更多详细信息的示例
https://www.suchan.cz/2015/08/uwp-quick-tip-getting-device-os-and-app-info/
最后我发现调用本机函数是最好的解决方案。
Link: really good pinvoke site
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Monitoring
{
public static class NativeCallsWrapper
{
private static SYSTEM_INFO sysInfo = new SYSTEM_INFO();
private static MEMORYSTATUSEX mem = new MEMORYSTATUSEX();
[DllImport("kernel32.dll", SetLastError = false)]
public static extern void GetSystemInfo([In, Out] SYSTEM_INFO Info);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
static NativeCallsWrapper()
{
GetSystemInfo(sysInfo);
GlobalMemoryStatusEx(mem);
}
[StructLayout(LayoutKind.Explicit)]
public struct SYSTEM_INFO_UNION
{
[FieldOffset(0)]
public UInt32 OemId;
[FieldOffset(0)]
public UInt16 ProcessorArchitecture;
[FieldOffset(2)]
public UInt16 Reserved;
}
public struct SYSTEM_INFO
{
public SYSTEM_INFO_UNION CpuInfo;
public UInt32 PageSize;
public UInt32 MinimumApplicationAddress;
public UInt32 MaximumApplicationAddress;
public UInt32 ActiveProcessorMask;
public UInt32 NumberOfProcessors;
public UInt32 ProcessorType;
public UInt32 AllocationGranularity;
public UInt16 ProcessorLevel;
public UInt16 ProcessorRevision;
}
[StructLayout(LayoutKind.Sequential)]
public class MEMORYSTATUSEX
{
public uint dwLength;
public uint dwMemoryLoad;
public ulong ullTotalPhys;
public ulong ullAvailPhys;
public ulong ullTotalPageFile;
public ulong ullAvailPageFile;
public ulong ullTotalVirtual;
public ulong ullAvailVirtual;
public ulong ullAvailExtendedVirtual;
public MEMORYSTATUSEX()
{
this.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
}
}
public static GeneralStatistics getGeneralStatistics()
{
GeneralStatistics generalStatistics = new GeneralStatistics();
generalStatistics.numberOfProcesses = (int)sysInfo.NumberOfProcessors;
generalStatistics.memoryTotal = mem.ullTotalPhys / 1048;
generalStatistics.memoryInUse = (mem.ullTotalPhys - mem.ullAvailPhys) / 1048;
return generalStatistics;
}
}
}
我们需要将我们拥有的应用程序升级到通用 Windows 应用程序 (UWP) 框架。该应用程序的主要目的是收集诊断统计信息并通过 rest 调用将它们发送回我们的服务器。
但是我找不到任何 api 可用于获取统计信息
之前我们是
PerformanceCounter _memoryCounter = new PerformanceCounter();
public SystemProperty GetPhysicalMemory()
{
string s = _QueryComputerSystem("totalphysicalmemory");
double totalphysicalmemory = Convert.ToDouble(s);
double d = _GetCounterValue(_memoryCounter, "Memory", "Available Bytes", null);
return new SystemProperty { PropertyName = "Physical Memory", Total = totalphysicalmemory, Used = totalphysicalmemory - d };
}
返回已用和可用内存的总字节数。我们还收集了网络和 cpu 统计数据。 None 其中与新框架兼容。我应该为这个功能寻找什么命名空间?或者我需要使用 Pinvoke (
您可以使用 MemoryManager 获取设备的内存
https://msdn.microsoft.com/en-us/library/windows.system.memorymanager.aspx
还可以获取设备信息
https://msdn.microsoft.com/en-us/library/windows/apps/windows.system.profile.aspx
更新:这是获取当前设备更多详细信息的示例 https://www.suchan.cz/2015/08/uwp-quick-tip-getting-device-os-and-app-info/
最后我发现调用本机函数是最好的解决方案。 Link: really good pinvoke site
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Monitoring
{
public static class NativeCallsWrapper
{
private static SYSTEM_INFO sysInfo = new SYSTEM_INFO();
private static MEMORYSTATUSEX mem = new MEMORYSTATUSEX();
[DllImport("kernel32.dll", SetLastError = false)]
public static extern void GetSystemInfo([In, Out] SYSTEM_INFO Info);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
static NativeCallsWrapper()
{
GetSystemInfo(sysInfo);
GlobalMemoryStatusEx(mem);
}
[StructLayout(LayoutKind.Explicit)]
public struct SYSTEM_INFO_UNION
{
[FieldOffset(0)]
public UInt32 OemId;
[FieldOffset(0)]
public UInt16 ProcessorArchitecture;
[FieldOffset(2)]
public UInt16 Reserved;
}
public struct SYSTEM_INFO
{
public SYSTEM_INFO_UNION CpuInfo;
public UInt32 PageSize;
public UInt32 MinimumApplicationAddress;
public UInt32 MaximumApplicationAddress;
public UInt32 ActiveProcessorMask;
public UInt32 NumberOfProcessors;
public UInt32 ProcessorType;
public UInt32 AllocationGranularity;
public UInt16 ProcessorLevel;
public UInt16 ProcessorRevision;
}
[StructLayout(LayoutKind.Sequential)]
public class MEMORYSTATUSEX
{
public uint dwLength;
public uint dwMemoryLoad;
public ulong ullTotalPhys;
public ulong ullAvailPhys;
public ulong ullTotalPageFile;
public ulong ullAvailPageFile;
public ulong ullTotalVirtual;
public ulong ullAvailVirtual;
public ulong ullAvailExtendedVirtual;
public MEMORYSTATUSEX()
{
this.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
}
}
public static GeneralStatistics getGeneralStatistics()
{
GeneralStatistics generalStatistics = new GeneralStatistics();
generalStatistics.numberOfProcesses = (int)sysInfo.NumberOfProcessors;
generalStatistics.memoryTotal = mem.ullTotalPhys / 1048;
generalStatistics.memoryInUse = (mem.ullTotalPhys - mem.ullAvailPhys) / 1048;
return generalStatistics;
}
}
}