public static void CreateRecordLogEntry <RecordLogType>(this DCDContext dc, int recordId, ImportLog importLog, RecordImportResult result, RecordImportOperation operation, string notes, Exception e) where RecordLogType : IDCDRecordImportLog, new()
        {
            try
            {
                RecordLogType log = new RecordLogType();
                log.RecordId  = recordId;
                log.ImportLog = dc.ImportLogs.FirstOrDefault(w => w.Id == importLog.Id);
                log.Result    = result.ToString();
                log.Operation = operation.ToString();
                log.Notes     = notes + (e != null ? $"[{e.ToString()}]" : string.Empty);
                log.TimeStamp = DateTime.Now;
                log.InsertRecordOnSubmit(dc);
                dc.SubmitChanges();
            }
            catch (Exception ex)
            {
                DCDImportLogger.Error("Error writing log message to database", ex);
            }

            if (result == RecordImportResult.Failure)
            {
                if (e != null)
                {
                    DCDImportLogger.Error("Error processing record " + recordId + ".  " + notes, e);
                }
                else
                {
                    DCDImportLogger.Error("Error processing record " + recordId + ".  " + notes);
                }
            }
            else
            {
                DCDImportLogger.Info("Successfully processed record " + recordId + ".");
            }
        }
        private void CreateWatcher()
        {
            DCDImportLogger.Debug("Attempting to start FileWatcher.");
            //Create a new FileSystemWatcher.
            FileSystemWatcher watcher = new FileSystemWatcher();

            //If import directory is invalid do not continue, because the watcher depends on it and the ProcessingDirectory depends on it too.
            if (Directory.Exists(ImportDirectory) == false)
            {
                return;
            }

            //Set the filter to only catch xml files.
            watcher.Filter   = "*.xml";
            watcher.Changed += Watcher_FileChanged;
            watcher.Path     = ImportDirectory;

            //Enable the FileSystemWatcher events.
            watcher.EnableRaisingEvents = true;
            watcher.NotifyFilter        = NotifyFilters.LastWrite;

            DCDImportLogger.Info("FileWatcher for DCD Import Started.");

            //Process any files that are in the "processing" folder
            //This is to re-process anything that was aborted during an app pool recycle
            DCDImportLogger.Debug("Checking path [" + ProcessingDirectory + "]");
            if (Directory.Exists(ProcessingDirectory))
            {
                string[] files = Directory.GetFiles(ProcessingDirectory, "*.xml");
                DCDImportLogger.Debug("Found [" + files.Length + "] files with filter *.xml");

                //process each file
                foreach (string file in files)
                {
                    Action <string> processAction = path => processFile(path);

                    DCDImportLogger.Debug("Processing existing file [" + file + "]");
                    processAction.BeginInvoke(file, null, null);
                }
            }
        }
        private void Watcher_FileChanged(object sender, FileSystemEventArgs e)
        {
            string fullPath = e.FullPath;
            string name     = e.Name;

            DCDImportLogger.Debug("FileChanged event raised for [" + name + "] found at [" + fullPath + "]");

            if (!Directory.Exists(DestinationDirectory))
            {
                Directory.CreateDirectory(DestinationDirectory);
            }

            if (!Directory.Exists(ProcessingDirectory))
            {
                Directory.CreateDirectory(ProcessingDirectory);
            }

            //If the file does not exist, exit out
            //This event can be fired when a file is deleted, so we need this check
            if (!File.Exists(fullPath))
            {
                return;
            }
            DCDImportLogger.Info(string.Format("Processing File: " + name));

            // check number one to make sure file is done uploading
            if (XMLFileUtilities.IsOpen(fullPath))
            {
                bool bFileOpen = true;
                while (bFileOpen)
                {
                    if (XMLFileUtilities.IsOpen(fullPath))
                    {
                        DCDImportLogger.Info(string.Format("File is open, Waiting 2 seconds and looping..."));
                        Thread.Sleep(2000);
                    }
                    else
                    {
                        bFileOpen = false;
                        DCDImportLogger.Info(string.Format("File is available... continue processing."));
                    }
                }
            }

            //clean bad characters from the file
            string       processingName     = "Processing_" + name.Replace(".xml", "") + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xml";
            string       processingFullPath = ProcessingDirectory + processingName;
            StreamReader reader             = new StreamReader(fullPath, Encoding.GetEncoding("Windows-1252"));
            StreamWriter writer             = new StreamWriter(processingFullPath, false, Encoding.UTF8);
            bool         first = true;

            while (true)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }
                if (first && line.Contains("<?xml version"))
                {
                    writer.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                    first = false;
                }
                else
                {
                    writer.WriteLine(XMLFileUtilities.SanitizeXmlString(line));
                }
            }
            reader.Close();
            writer.Close();

            //Copy the original file to the archive folder
            string processingDestPath = DestinationDirectory + processingName;

            File.Copy(fullPath, processingDestPath);
            //Delete the original file
            File.Delete(fullPath);

            processFile(processingFullPath);
        }