BPSoftware.com
Home   Utilities   Purchase   FAQ   Support   Contact        
Shareware Utilities
 APrintDirect
 AIconExtract
 AFile Attribute Manager
Freeware Utilities
 AddrMon
 AFileSync
 ASysIcon
 B&P Table Utilities
 BPACLer
 BPSNMPMon
 BPSNMPUtil
 CharCount
 Delphi® Components
 MacAddr
Miscellaneous
 BPSoftware Blog
 Conversion Utility
 Rock Paper Scissors Game
 Random Number Generator
 DayLightTime
 BlogRoll
 Purchase Shareware
 Support
 Privacy

 


Thursday, November 22, 2007
A little System.Diagnostics

I 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:


1namespace BPSoftware
2{
3 public class Logging
4 {
5 /// <summary>
6 /// strNameSpace is the base namespace for use in the logging message.
7 /// </summary>

8 public static string strNameSpace;
9
10 /// <summary>
11 /// GeneralTraceSwitch is a traceswitch object for logging. Switch level is set through
12 /// the application config switches section. The destination listener is also set through
13 /// the application config file.
14 /// </summary>

15 public static TraceSwitch GeneralTraceSwitch = new TraceSwitch( "General", "General Trace Switch" );
16
17 Methods
86 }

87}

Throughout the application logging messages can be added:
1 catch ( Exception ex)
2 {
3 Logging.WriteLineTraceError( "ClassName", "Method", "Exception: " + ex.Message );
4 }

5
6 Logging.WriteLineTraceInfo( "ClassName", "Method",,
7 String.Format( "{0}: {1}", e.ActionType.ToString(), e.Message )
8 );


The application’s config file can then be altered to determine the appropriate level and location of the logging based upon certain diagnostic needs. The switches node specifies what level of logging should occur. The switches operate in a hierarchial fashion (3 includes 3, 2 and 1; 2 includes 2 and 1):
1 <system.diagnostics>
2 <trace autoflush="true" indentsize="4">
3 <!-- Specify listener output -->
4 <listeners>
5 <remove name="Default" />
6 <!-- Write to a log file -->
7 <add name="Default"
8 type="System.Diagnostics.TextWriterTraceListener"
9 initializeData="AFileSync.log" />
10 <!-- Write to the Event Log-->
11 <!-- <add name="Default"
12 type="System.Diagnostics.EventLogTraceListener"
13 initializeData="Application" /> -->
14 </listeners>
15 </trace>
16 <switches>
17 <!-- Off = 0, Error = 1, Warning = 2, Info = 3, Verbose = 4 -->
18 <add name="General" value="3" />
19 </switches>
20 </system.diagnostics>


Using the application’s config file to specify the level of logging allows for the capture of diagnostic information of a stable build to capture ‘real world’ information. The flexibility of the listener to write to (you can create customer listeners) is an added bonus!

Labels: , , ,

posted by Brad Prendergast at 8:49:00 AM (0 comments)
Links to this post
Permalink
Sunday, October 07, 2007
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.


1using System;
2using System.Windows.Forms;
3using System.ServiceProcess;
4
5namespace Services1
6{
7 public partial class Form1 : Form
8 {
9 ServiceController controller;
10
11 public Form1()
12 {
13 controller = new ServiceController();
14 controller.MachineName = ".";
15 InitializeComponent();
16 lstServices.DisplayMember = "DisplayName";
17 lstServices.ValueMember = "ServiceName";
18 lstServices.DataSource = ServiceController.GetServices();
19 }

20
21 private void StartService(string servicename)
22 {
23 controller.ServiceName = servicename;
24 controller.Start();
25
26 }

27
28 private void PauseService(string servicename)
29 {
30 controller.ServiceName = servicename;
31 if (controller.CanPauseAndContinue)
32 controller.Pause();
33 }

34
35 private void StopService(string servicename)
36 {
37 controller.ServiceName = servicename;
38 if (controller.CanStop)
39 controller.Stop();
40 }

41
42
43 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
44 {
45 if (lstServices.SelectedItems.Count > 0)
46 {
47 try
48 {
49 controller.ServiceName = lstServices.SelectedValue.ToString();
50 textBox1.Text = String.Format(
51 "Service Name: {0}\r\nDisplay Name: {1}\r\nType: {2}\r\nStatus: {3}",
52 controller.ServiceName,
53 controller.DisplayName,
54 controller.ServiceType.ToString(),
55 controller.Status.ToString()
56 );
57 }

58 catch (Exception ex)
59 {
60 textBox1.Text = String.Format("Error: \r\n{0}",ex.Message);
61 }

62 }

63 else
64 {
65 textBox1.Clear();
66 }

67 }

68
69 private void btnStart_Click(object sender, EventArgs e)
70 {
71 StartService(lstServices.SelectedValue.ToString());
72 }

73
74 private void btnPause_Click(object sender, EventArgs e)
75 {
76 PauseService(lstServices.SelectedValue.ToString());
77 }

78
79 private void btnStop_Click(object sender, EventArgs e)
80 {
81 StopService(lstServices.SelectedValue.ToString());
82 }

83 }

84}

Labels: , ,

posted by Brad Prendergast at 3:12:00 PM (0 comments)
Links to this post
Permalink
Monday, October 01, 2007
Let's Synchronize Some Files

Recently, 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:


1using System;
2using System.IO;
3using System.Diagnostics;
4
5namespace BPSoftware
6{
7 class FileSync
8 {
9 private FileSystemWatcher watcher;
10
11 Fields