Wednesday, February 9, 2011

Removing a log4net log file with a custom uninstaller

After creating a Windows Installer Setup Project to install a basic forms application demo application testing found that the program files directory that the installer created was not removed due to a log file that was created by log4net.

To resolve the issue a Installer Class was added to the winforms project with an override for Uninstall. Adding the custom Installer class to the existing project ensures that is was included in the Application Folder for the target machines File System without bringing in another DLL.

[RunInstaller(true)]
 public partial class Installer1 : System.Configuration.Install.Installer
 {
  public Installer1()
  {
   InitializeComponent();
  }

  [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
  public override void Uninstall(IDictionary savedState)
  {
   base.Uninstall(savedState);

   //This is handy if you need to attach the debugger to the custom install action.
   //System.Diagnostics.Debugger.Break();

   try
   {
    string basePath = this.Context.Parameters["assemblyPath"];

    string baseDirectory = Path.GetDirectoryName(basePath);

    string logFilePath = Path.Combine(baseDirectory, "testapp.log");
    if (File.Exists(logFilePath))
    {
     File.Delete(logFilePath);
    }
   }
   catch
   {
    //Failed to delete the log file
   }

  }

 }

Finally the custom Uninstall implementation is connected to the Windows Installer.