A little System.DiagnosticsI am not going to get overly verbose here; however the importance of application logging is immeasurable. When applications are deployed, obtaining ‘real life’ application can assist in diagnostic and overall application health situations. Excessive logging can be ineffective (too much to analyze) and burdensome (logging to a text file that grows forever) if not implemented appropriately.
When developing and deploying an application having the flexibility of what, when and how to log can alleviate the burden and ineffectiveness of logging. The System.Diagnostics namespace has a number of classes available that allow for this type of flexible logging. The applied use of these classes allows a developer to specify what to log in an application at various levels of detail within one application without the need to deploy diagnostic builds. Depending on diagnostic needs, the application’s config file can specify the level of diagnostic information to write and where. A simple Logging class (this is the class I created to use this functionality) can be developed as follows:
namespace BPSoftware
...{
public class Logging
...{
/**//// <summary>
/// strNameSpace is the base namespace for use in the logging message.
/// </summary>
public static string strNameSpace;

/**//// <summary>
/// GeneralTraceSwitch is a traceswitch object for logging. Switch level is set through
/// the application config switches section. The destination listener is also set through
/// the application config file.
/// </summary>
public static TraceSwitch GeneralTraceSwitch = new TraceSwitch( "General", "General Trace Switch" );

Methods#region Methods
/**//// <summary>
/// Writeline is the base logging method. Messages are logged to the listener specified
/// in the application config file.
/// </summary>
/// <param name="strClassName">Calling class name.</param>
/// <param name="strMethodName">Calling method name.</param>
/// <param name="strMessage">The message sent to the listener.</param>
public static void WriteLine( string strClassName, string strMethodName, string strMessage )
...{
lock ( typeof( Logging ) )
...{
Trace.WriteLine( string.Concat(
new object[] ...{
"(", DateTime.Now, ") ",
strNameSpace, ".", strClassName, ".", strMethodName, " -->", strMessage
}
) );
}
}

/**//// <summary>
/// Pass information to the base WriteLine if the TraceError switch is set.
/// </summary>
/// <param name="strClassName">Calling class name.</param>
/// <param name="strMethodName">Calling method name.</param>
/// <param name="strMessage">The message sent to the listener.</param>
public static void WriteLineTraceError( string strClassName, string strMethodName, string strMessage )
...{
if ( Logging.GeneralTraceSwitch.TraceError )
WriteLine( strClassName, strMethodName, strMessage );
}

/**//// <summary>
/// Pass information to the base WriteLine if the TraceWarning switch is set.
/// </summary>
/// <param name="strClassName">Calling class name.</param>
/// <param name="strMethodName">Calling method name.</param>
/// <param name="strMessage">The message sent to the listener.</param>
public static void WriteLineTraceWarning( string strClassName, string strMethodName, string strMessage )
...{
if ( Logging.GeneralTraceSwitch.TraceWarning )
WriteLine( strClassName, strMethodName, strMessage );
}

/**//// <summary>
/// Pass information to the base WriteLine if the TraceInfo switch is set.
/// </summary>
/// <param name="strClassName">Calling class name.</param>
/// <param name="strMethodName">Calling method name.</param>
/// <param name="strMessage">The message sent to the listener.</param>
public static void WriteLineTraceInfo( string strClassName, string strMethodName, string strMessage )
...{
if ( Logging.GeneralTraceSwitch.TraceInfo )
WriteLine( strClassName, strMethodName, strMessage );
}

/**//// <summary>
/// Pass information to the base WriteLine if the TraceVerbose switch is set.
/// </summary>
/// <param name="strClassName">Calling class name.</param>
/// <param name="strMethodName">Calling method name.</param>
/// <param name="strMessage">The message sent to the listener.</param>
public static void WriteLineTraceVerbose( string strClassName, string strMethodName, string strMessage )
...{
if ( Logging.GeneralTraceSwitch.TraceVerbose )
WriteLine( strClassName, strMethodName, strMessage );
}
#endregion
}
}
catch ( Exception ex)
...{
Logging.WriteLineTraceError( "ClassName", "Method", "Exception: " + ex.Message );
}
Logging.WriteLineTraceInfo( "ClassName", "Method",,
String.Format( "{0}: {1}", e.ActionType.ToString(), e.Message )
);
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<!-- Specify listener output -->
<listeners>
<remove name="Default" />
<!-- Write to a log file -->
<add name="Default"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="AFileSync.log" />
<!-- Write to the Event Log-->
<!-- <add name="Default"
type="System.Diagnostics.EventLogTraceListener"
initializeData="Application" /> -->
</listeners>
</trace>
<switches>
<!-- Off = 0, Error = 1, Warning = 2, Info = 3, Verbose = 4 -->
<add name="General" value="3" />
</switches>
</system.diagnostics>
Wi-Fi Detector Shirt
Labels: Information, Internet, Misc
Control and List Windows Services
As I whipped up my file synchronizing service application, to synchronize files between my computer and my NAS), I worked with the System.ServiceProcess Namespace. The System.ServiceProcess Namespace "provides classes that allow you to implement, install, and control Windows service applications".
My file synchronization needs did not require that my application service to be "running" all the time. In, fact I only needed the service to run under certain situations. It was easy enough to get the service written and installed and now I wanted to 'automatically' (programmatically) start and stop my service when certain conditions were met.
Entrance - the ServiceController Class. The ServiceController Class "Represents a Windows service and allows you to connect to a running or stopped service, manipulate it, or get information about it." In order to get acclimated to the ServiceController Class I created a simple application to list and display information about the installed services.
using System;
using System.Windows.Forms;
using System.ServiceProcess;
namespace Services1
...{
public partial class Form1 : Form
...{
ServiceController controller;
public Form1()
...{
controller = new ServiceController();
controller.MachineName = ".";
InitializeComponent();
lstServices.DisplayMember = "DisplayName";
lstServices.ValueMember = "ServiceName";
lstServices.DataSource = ServiceController.GetServices();
}
private void StartService(string servicename)
...{
controller.ServiceName = servicename;
controller.Start();
}
private void PauseService(string servicename)
...{
controller.ServiceName = servicename;
if (controller.CanPauseAndContinue)
controller.Pause();
}
private void StopService(string servicename)
...{
controller.ServiceName = servicename;
if (controller.CanStop)
controller.Stop();
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
...{
if (lstServices.SelectedItems.Count > 0)
...{
try
...{
controller.ServiceName = lstServices.SelectedValue.ToString();
textBox1.Text = String.Format(
"Service Name: {0}\r\nDisplay Name: {1}\r\nType: {2}\r\nStatus: {3}",
controller.ServiceName,
controller.DisplayName,
controller.ServiceType.ToString(),
controller.Status.ToString()
);
}
catch (Exception ex)
...{
textBox1.Text = String.Format("Error: \r\n{0}",ex.Message);
}
}
else
...{
textBox1.Clear();
}
}
private void btnStart_Click(object sender, EventArgs e)
...{
StartService(lstServices.SelectedValue.ToString());
}
private void btnPause_Click(object sender, EventArgs e)
...{
PauseService(lstServices.SelectedValue.ToString());
}
private void btnStop_Click(object sender, EventArgs e)
...{
StopService(lstServices.SelectedValue.ToString());
}
}
}
Let's Synchronize Some FilesRecently, I posted about the new addition to my backup strategy; a NAS. My intended use of this device is to have a hot backup of my files. The system I was using before was not capable of storing the amount of data I need backed up, nor was it easy to manage.
I modified the set of xCopy batch files that I used for my backups to write the files to the NAS.
This system worked fine, but I needed more. I manage a lot of data and have a lot of files. I wouldn't want to lose any in the event of a system failure. The one problem I see with using this process is the lack of a real time solution.
I was in search of a solution that would offer me real-time synchronization between my files and the NAS. I didn't have to search very far. The .NET Framework includes the FileSystemWatcher Class.
The FileSystemWatcher Class "Listens to the file system change notifications and raises events when a directory, or file in a directory, changes." (quoted directly from MSDN) After reading through this class I quickly whipped up a solution. I developed my own FileSync class that allowed me to easily synchronize files between my remote and local storage. I created a Windows Service (threaded) built around my class (the screen shot in this post is from a sample app I used to test my class) so that I could have the FileSync class running and keeping my files in sync without me needing to do anything. Here is the unit I created:
using System;
using System.IO;
using System.Diagnostics;
namespace BPSoftware
...{
class FileSync
...{
private FileSystemWatcher watcher;

Fields#region Fields
private string destpath;
public string DestPath
...{
get ...{ return destpath; }
set ...{ destpath = value; }
}
private string sourcepath;
public string SourcePath