Spike exploration: sftp, zip, config and Windows Service

I did a spike exploration to find out what is involved in creating a Windows Service with event logging. I also wanted to find out what is involved in working with sftp, zip and configuration with C#.  The code is available on github: https://github.com/k0emt/sftp-windows-service-spike

sftp

In order to test the sftp code I set up a Linux VM in the Azure cloud environment and created a user account on it.

I use the ssh component from http://sshnet.codeplex.com/ for sftp.  It is very straight forward to use this component.  The key for sftp is to use the SftpClient class.  Upload a file with the UploadFile method.  The sample code is in the ConsoleSftp projects Program.cs: https://github.com/k0emt/sftp-windows-service-spike/blob/clean/ConsoleSftp/Program.cs

zip

Use System.IO.Compression for creating/expanding zip files.

To use the ZipFile class, you must reference the System.IO.Compression.FileSystem assembly in your project.

http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile(v=vs.110).aspx

I use ZipFile.CreateFromDirectory in the code above to zip an entire directory before sending it over sftp.

configuration

ConfigurationManager is the class that is used for working with application settings.

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v=vs.110).aspx

I used it in the service code to pull in a setting called user: https://github.com/k0emt/sftp-windows-service-spike/blob/clean/TransferService/TransferService.cs

In order for this to come together, you must create an app.config file in your project and change the property setting in Visual Studio to tell it to deploy when the project is built. https://github.com/k0emt/sftp-windows-service-spike/blob/clean/TransferService/App.config

Windows service with event logging

Create a Windows Service by choosing the Windows Service project template.

The Windows Service code must be installed and uninstalled.  It is possible to create an installer/uninstaller.  I opted to use the command line for this spike.

http://msdn.microsoft.com/en-us/library/y817hyb6(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx

I went here to learn about Service Methods – that is the kind of events to which the service can respond.  You can use the Visual Studio designer to enable the events you want handle.  Then you’ll need to code the events.  For this spike code I have it make an entry in the event log for each event type that is triggered.

http://msdn.microsoft.com/en-us/library/System.ServiceProcess.ServiceBase_methods(v=vs.110).aspx

Installing and Uninstalling the service

http://msdn.microsoft.com/en-us/library/sd8zc8ha(v=vs.110).aspx

Install with installutil.exe TransferService.exe

image

Go to Windows Services to see that the installed service is running.  See, the Spike Transfer Service below.

image

Uninstalling with installutil.exe /u TransferService.exe

image

Event Logging

For event logging a new EventLog is created.  An event source must be created and the source has to be wired up to the EventLog. 

Once this is done, the WriteEntry method can be used for writing to the event log.

You can go to the system event log panel to see where the events have been logged.

image

Summary

Overall, I was pleasantly surprised with the ease of accomplishing the spikes target tasks.  There was also decent documentation and examples available.

Oracle SQL Developer Date/Time format

One of the most super annoying things about Oracle SQL developer is that it defaults to a not so useful (at least to me) date format along the lines of 15-NOV-11.  I prefer to see something helpful like an  ISO 8601 formatted date/time.

The solution is to, in Oracle SQL Developer, go to: Tools > Preferences.

Then in the Preferences dialog, select Database and drill down to NLS.

In the NLS parameters you'll be able to set the Date Format to:
YYYY-MM-DD"T"HH24.MI.SSFFTZR"Z"

I have found that on some systems, you may need to use a setting like:  
YYYY-MM-DD HH24.MI.SS



References:

http://stackoverflow.com/questions/8134493/how-can-i-set-a-custom-date-time-format-in-oracle-sql-developer

http://orastory.wordpress.com/2007/05/15/dates-timestamps-and-iso-8601/

git: purging the lurking big file

I was just working with an old repository where I had inadvertently committed a large file ~300MB.  I subsequently removed it, but not in a way that took it out of history.  I discovered it when I went to push to GitHub and it complained.  These are the steps I used to get rid of the file:

  • git filter-branch --prune-empty -f --index-filter 'git rm -rf --cached --ignore-unmatch source/spikes/mo_offender_fak930.txt'
    -tag-name-filter cat -- --all
  • git update-ref -d refs/original/refs/heads/master
  • git reflog expire --expire=now –all
  • git gc --prune=now

Here are some pages I found useful to figure this all out:

http://stackoverflow.com/questions/6017987/is-there-a-way-in-git-to-list-all-deleted-files-in-the-repository

http://stackoverflow.com/questions/2100907/how-to-remove-delete-a-large-file-from-commit-history-in-git-repository

http://naleid.com/blog/2012/01/17/finding-and-purging-big-files-from-git-history