ProcessResourcesMonitor 1.0.1

dotnet add package ProcessResourcesMonitor --version 1.0.1
                    
NuGet\Install-Package ProcessResourcesMonitor -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ProcessResourcesMonitor" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ProcessResourcesMonitor" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ProcessResourcesMonitor" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ProcessResourcesMonitor --version 1.0.1
                    
#r "nuget: ProcessResourcesMonitor, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ProcessResourcesMonitor@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ProcessResourcesMonitor&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=ProcessResourcesMonitor&version=1.0.1
                    
Install as a Cake Tool

ProcessRessourcesMonitor

NuGet version

This package helps track resources used by a specific process over specific intervals of time and store the collected metrics in many formats.<br/> There are some good tools for this purpose (such as System.Diagnostics) but they only work perfectly on Windows, this package aims to support any OS (Windows, Linux, MacOs).

Table of Contents

Installation

To install the ProcessResourcesMonitor package, run the following command in your terminal:

dotnet add package ProcessResourcesMonitor --version 1.0.1

Features

  • Real-time Process Metrics Monitoring: Collect real-time metrics such as CPU usage, memory consumption, thread count, and private bytes for any running process.

  • Flexible Data Storage Options: Save collected metrics in multiple formats such as JSON, CSV, or in-memory storage to suit your application needs.

  • Customizable Collection Intervals: Choose the desired interval for collecting metrics, allowing for fine-tuned monitoring based on your performance requirements.

  • Easy-to-Use API: With simple-to-use methods, you can set up metric collection and storage with minimal code.

  • Status Tracking: Monitor the status of the process, ensuring you know whether it's running or has exited.

  • Pluggable Storage: Extendable storage interface lets you implement custom storage options (e.g., save data to databases or cloud storage).

  • Cross-Platform Compatibility: Built to work on Windows, Mac, and Linux, making it a versatile solution for monitoring processes in any environment.

Usage

To use the ProcessResourcesMonitor package, you can collect the metrics of any process by providing its process ID. This example demonstrates how to collect CPU and memory usage for a process with ID 652, store the metrics in memory, and save them as a JSON file.

using ProcessResourcesMonitor;
using ProcessResourcesMonitor.Storage;

class Program {
    public static async Task Main(string[] args) {
        // Create a MetricsCollector instance for the process with ID 652
        var collector = new MetricsCollector(652);

        // Use memory storage to temporarily store collected metrics
        var storage = new MetricsMemoryStorage();

        // Start collecting metrics every 500 milliseconds
        await collector.CollectMetricsAsync(TimeSpan.FromMilliseconds(500), metrics => {
            // Store the collected metrics in memory
            storage.store(metrics);

            // Display the metrics stored in memory
            Console.WriteLine(storage.getMetrics().ToString());

            // Save the collected metrics as a JSON file
            storage.saveAsJson("metrics.json");

            // Display CPU usage and memory usage for the process
            Console.WriteLine($"CPU usage and memory usage: {metrics.CpuUsage}% CPU, {metrics.MemoryUsage} MB Memory");
        });
    }
}

Your .json file where you want to store the metrics (metrics.json in the last example) will be updated in real time and will look like :

[
  {
    "Timestamp": 1728548899617,
    "CpuUsage": 0,
    "MemoryUsage": 17.609375,
    "ThreadCount": 6,
    "PrivateBytes": 0,
    "Status": "Running"
  },
  {
    "Timestamp": 1728548900158,
    "CpuUsage": 0,
    "MemoryUsage": 17.609375,
    "ThreadCount": 6,
    "PrivateBytes": 0,
    "Status": "Running"
  }
  // More metrics
]
CSV storage:

You can also store the metrics in a .csv file :

var collector = new MetricsCollector(652);

        // Use CSV storage to store collected metrics in 'metrics.csv'
        var storage = new MetricsCsvStorage("metrics.csv");

        // Start collecting metrics every 500 milliseconds
        await collector.CollectMetricsAsync(TimeSpan.FromMilliseconds(500), metrics => {
            // Store the collected metrics in the CSV file
            storage.store(metrics);
        });

and your .csv file will be updated in real time and will look like this :

Timestamp,CpuUsage,MemoryUsage(MB),ThreadCount,PrivateBytes,Status
1728550660181,0,16.34375,6,0,Running
1728550660685,0,16.34375,6,0,Running
1728550661186,0.0011394735582293427,16.34375,6,0,Running
Description of Fields:
  • Timestamp: The timestamp of when the metrics were collected.
  • CpuUsage: The percentage of CPU used by the process. MemoryUsage: The amount of memory (in MB) the process is using.
  • ThreadCount: The number of active threads in the process.
  • PrivateBytes: The amount of private memory the process is consuming.
  • Status: The current status of the process (e.g., "Running").

Contributing

Contributions are welcome! If you have ideas or want to contribute improvements, please follow these steps:

1 - Fork the repository.

2 - Create a new branch for your feature or fix:<br> git checkout -b feature/your-feature or git checkout -b fix/your-fix

3 - Commit your changes: <br> git commit -m "Add your message"

4 - Push to the branch:<br> git push origin your-branch.

5 - Create a pull request describing the changes you’ve made. Make sure to check the existing issues and feature requests before starting work on something new.

License

This project is licensed under the Apache License, Version 2.0. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 154 10/10/2024
1.0.0 122 10/6/2024

Initial release