I'm working on a small .NET Standard 2.0 logging library, and I'm having trouble finding a way to reliably get the memory used by the current process across all platforms, especially on UWP.
Right now I'm using this code (.NET Standard 2.0):
long memory = Process.GetCurrentProcess().PrivateMemorySize64;
Which works fine, but throws a nice PlatformNotSupportedException exception on UWP (actually, that's only in DEBUG mode, while in RELEASE is directly throws a TypeLoadException plus a bunch of other P/Invoke exceptions for some reason).
The problem here is that that API is obviously not supported on UWP, where I should use:
long memory = (long)MemoryManager.AppMemoryUsage;
The problem is that the MemoryManager is a UWP-only API, and it is not present in .NET Standard 2.0. Now, the first workaround that comes to mind is to expose a setting in the library to let the user manually set a custom Func<long> delegate that retrieves the current memory usage, so that if it knows the default method won't work on the current platform, it'll be possible to override it.
This seems like a bad trick though, and I'd like to keep everything self-contained withing the library instead. So my question is:
Is there a way to reliably retrieve the current process/app usage across any platform that supports .NET Standard 2.0 libraries?
Thanks!