2016-02-29 92 views
0

我们需要将我们的应用程序升级到通用Windows应用程序(UWP)框架。该应用程序的主要目的是收集诊断统计信息并通过其余呼叫将它们发送回我们的服务器。如何从UWP应用程序获取诊断统计信息

但是我没有发现任何的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统计信息。其中没有一个与新框架兼容。我应该在哪些名称空间中查看此功能?或者我需要使用Pinvoke(link)之类的东西?

回答

2

最后我发现调用本地函数是最好的解决方案。 链接: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; 
     } 
    } 
} 
1

可以使用的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/

+0

的MemoryManager是针对特定应用的统计信息,而不是系统作为一个整体。我正在继续使用Windows.System.Profile,但我没有得到任何有用的硬件信息系统 –

+0

请检查我更新了我的答案 – RicardoPons

+1

的示例谢谢您的更新,但该示例仍然是应用程序和操作系统信息。我需要整个系统的使用统计信息[网络,CPU和内存]。 –

相关问题