Send SMS Notifications on Application Performance Using C#

When working on applications, you would often want to log meaningful data, such as where your application has been failing, CPU usage, and Memory usage. This would provide you with various vital insights that would give your business a boost and help detect bugs quickly. To help you respond quickly, you can use notifications that contain a summary of the performance stats or details about an error in the production.

This article talks about how you can retrieve performance stats of an ASP.NET Core 5 web application using C# and then take advantage of the Vonage SMS API to send out text notifications at pre-configured intervals of time.

Prerequisites

  • Windows OS

You’ll also need to install the following NuGet packages onto your project:

  • NLog
  • Quartz
  • Vonage

You can install them via the NuGet Package Manager while you’re within Visual Studio.

Vonage API Account

The Starter Project

Using Quartz.NET for Background Jobs

The Core Components of Quartz.NET

A Job — This represents the background tasks that you want to run while you do other things. A job class extends the IJob interface and implements its members.

A Trigger — A trigger determines when a task is executed, and it is usually set to run on a predetermined schedule. In other words, you can use a trigger to specify the schedule of a job.

A Scheduler — This is a component responsible for coordinating the jobs and the triggers and executing jobs based on predefined schedules.

The blog will discuss more on these components shortly.

The Components of the Send Notification Application

  • Custom Scheduler — You’ll use Quartz to build a custom scheduler that would keep an eye on the memory and CPU consumption in the computer system. It would check these values at predefined intervals of time in the background while working with the application in the front-end.
  • Custom Logger — You’ll also be using NLog to log data about application performance in this application. While the notifications will be sent out only if CPU or memory consumption crosses the predefined threshold, the log would contain CPU and memory consumption data collected every 5 or 10 seconds (based on the predefined interval) and stored in a text file.

For this project, here are the classes that you’ll be creating in the application:

  • PerformanceHelper - This class is used to retrieve CPU and memory usage.
  • NotificationManager - this class encapsulates the functionality to send out notifications.
  • NotificationJob - This is the job class that takes advantage of the NotificationManager class to sends out notifications, as discussed earlier.
  • CustomJobFactory - This class is responsible for creating jobs.
  • CustomJobScheduler - This class is responsible for scheduling jobs.
  • QuartzExtensions - This class contains an extension method called AddQuartz to simplify and organize the code.

Use WMI to Retrieve CPU and Memory Usage

To retrieve the CPU and Memory usages in percentage, you’ll use the ManagementObjectSearcher class of the System Management namespace. This class can enumerate all disk drives, network adapters, processes, and other management objects on a system and retrieve a collection of management objects based on the query you've specified. When you instantiate this class, you'll need to pass a WMI query represented in an ObjectQuery and, optionally, a ManagementScope representing the WMI namespace to execute the query.

Create a class named PerformanceHelper.cs in the ASP.NET Core MVC 5 project you cloned earlier and write the following code:

Create a Job Class

The Execute method of the NotificationJob class calls the SendNotification method of the NotificationManager class. Create a new class named NotificationManager in a file called NotificationManager.cs with the following code in there:

The SendTextMessage function uses the Vonage package to send an SMS. SendTextMessage is called by SendResourceUsageInfo if the CPU or Memory usage has passed the threshold. You will set the thresholds and configuration variables later.

Create a Custom JobFactory Class

Create the CustomJobScheduler Class

The ScheduleJob method of the CustomJobScheduler class accepts an instance of type IScheduler and uses it to schedule a job based on a given trigger, which in this case is a TimeSpan.

Setting Up the Dependencies

As shown in the component diagram shown earlier, the QuartzExtensions class contains a method named AddQuartz. The method creates instances of JobFactory and NotificationJob, which both have a singleton lifespan. AddQuartz adds them both to the container. It also assigns our custom JobFactory class to the scheduler instance.

Configure the Application

"AllowedHosts": "*","Memory_Threshold": 80,"CPU_Threshold": 80,"API_KEY": "API_KEY","API_SECRET": "API_SECRET"

You should also configure NLog.config and specify the log file name and path there.

Add the following line in the ConfigureServices method of the Startup class to add an instance of the NotificationManager class as a transient service to the built-in services container:

Schedule the Job

Write the following code in the Configure method of the Startup class:

Run the Application

  1. Locate the .sln file and double click to open it in Visual Studio 2019
  2. Press F5 to run the application

Once the application is running, wait for the application to generate logs. You can see log messages being created every 5 seconds in the C:\\Logs folder. You can change this duration as needed by making it configurable in the appsettings.json file. You'll also see notifications sent to the pre-configured mobile number using the Vonage SMS API as soon as the CPU or Memory consumption crosses the threshold values.

You can check out a completed version of the project on GitHub under the completed folder.

Developer content from the team at Vonage, including posts on our Java, Node.js, Python, DotNet, Ruby and Go SDKs