Today, I had to implement custom performance counters in one of our applications. I haven’t worked much with performance counters before, so I wanted to check the Internet for some good articles and maybe some helper classes or wrappers around the functionality.

I found this article, which does an excellent job explaining how to go about it. But it didn’t offer any helper class or plug ‘n play wrapper, so I decided to build my own. Then I can use it the next time I have to implement performance counters in .NET.

The code is not very complicated, and I think it is pretty self explanatory. It consist of a helper class and a method that calls this helper class.

#region Using

using System;
using System.Diagnostics;

#endregion

namespace PerformanceCounters
{

/// <summary>
/// A helper class to create the specified performance counters.
/// </summary>
public class PerfmormanceMonitor
{

/// <summary>
/// Creates an instance of the class.
/// </summary>
/// <param name="categoryName">The name of the performance counter category.</param>
public PerfmormanceMonitor(string categoryName)
{
   this._Category = categoryName;
}

private CounterCreationDataCollection _Counters = new CounterCreationDataCollection();
private string _Category = string.Empty;

/// <summary>
/// Creates the performance counters
/// </summary>
public void CreateCounters()
{
   if (!PerformanceCounterCategory.Exists(_Category))
   { 
      PerformanceCounterCategory.Create(this._Category, this._Category, PerformanceCounterCategoryType.Unknown, this._Counters);
   }
}

/// <summary>
/// Add a performance counter to the category of performance counters.
/// </summary>
public void AddCounter(string name, string helpText, PerformanceCounterType type)
{
   CounterCreationData ccd = new CounterCreationData();
   ccd.CounterName = name;
   ccd.CounterHelp = helpText;
   ccd.CounterType = type;
   this._Counters.Add(ccd);
}

}
}

And here is the method that uses the helper class:

using System.Diagnostics;

static void CreatePerformanceCounter()
{
   PerfmormanceMonitor mon = new PerfmormanceMonitor("Headlight Parser");
   mon.AddCounter("# operations executed", "Total number of executed commands", PerformanceCounterType.NumberOfItems64);
   mon.AddCounter("# logfiles parsed", "Total number of logfiles parsed", PerformanceCounterType.NumberOfItems64);
   mon.AddCounter("# operations / sec", "Number of operations executed per second", PerformanceCounterType.RateOfCountsPerSecond32);
   mon.AddCounter("average time per operation", "Average duration per operation execution", PerformanceCounterType.AverageTimer32);
   mon.AddCounter("average time per operation base", "Average duration per operation execution base", PerformanceCounterType.AverageBase);
   mon.CreateCounters();
}

The functionality could be greatly expanded in the helper class, but I haven't got the time for it at the moment.

Sometimes you just want a simple function to perform a simple task. There are a lot of FTP libraries for free download on the Internet, but if you simply want to upload a file to an FTP server in C#, these libraries are overkill for that simple little task. That's what I thought when I browsed the web for such a simple little function. Maybe I'm slower than normal people, but I couldn't find any simple method on the web. They where all too complicated, so I thought to myself that I could do better.

Here's what I came up with:

private static void Upload(string ftpServer, string userName, string password, string filename)
{
   using (System.Net.WebClient client = new System.Net.WebClient())
   {
      client.Credentials = new System.Net.NetworkCredential(userName, password);
      client.UploadFile(ftpServer + "/" + new FileInfo(filename).Name, "STOR", filename);
   }
}

Then call the method with the right parameters, and you're set to go:

Upload("ftp://ftpserver.com", "TheUserName", "ThePassword", @"C:\file.txt");

Can it get any simpler than that?