Ejemplo n.º 1
0
        private void SendLogToServer(object data)
        {
            //only accept eventlog data
            if (!(data is EventLog))
            {
                return;
            }
            SendStatus.IsActive = true;

            //cast generic data to what we actually need
            EventLog newLog = data as EventLog;

            //find all logs that haven't been handled (submitted)
            List<EventLog> logsToBeSaved = null;

            //request exclusive access to our cache of existing logs
            lock (_cache)
            {
                //get pending records
                logsToBeSaved = GetLogsFromCache();

                //add new log to list
                logsToBeSaved.Add(newLog);

                //clear out cache
                SaveLogsToCache(new List<EventLog>());
            }

            //reorder by date received (created in our case)
            logsToBeSaved = logsToBeSaved.OrderBy(l => l.DateReceived).ToList();

            //loop through each log to be saved, give a dummy ID number
            int counter = 1;
            foreach (EventLog log in logsToBeSaved)
            {
                log.Id = counter;
                counter++;
            }

            //update our send status with the number of logs that we
            //plan to submit
            SendStatus.NumberOfTransmissions = logsToBeSaved.Count;

            //will hold the list of saved logs
            List<int> savedLogs = new List<int>(logsToBeSaved.Count);

            //send logs to the server
            foreach (EventLog log in logsToBeSaved)
            {
                //reset the log's sending user just to be safe
                _logger.WriteToLog(string.Format("Sending log with ID {0} to the server", log.Id), LogPriority.LowPriority);
                SendStatus.CurrentTransmission = log;

                try
                {
                    //the number that comes back from the web service is the log's local ID number.  Save
                    //for later when we clean up our local db.
                    int result = -1;
                    lock (_cache)
                    {
                        string webServiceKey = _cache[StringConstants.AuthenticationCacheKey] as string;
                        result = _webServiceClient.SubmitLog(log, webServiceKey);
                    }
                    savedLogs.Add(result);

                    //update our submission status
                    SendStatus.LastTransmissionTime = DateTime.UtcNow;
                    SendStatus.LastTransmission = log;
                    SendStatus.CompletedTransmissions++;
                }
                catch (Exception ex)
                {
                    SendError(ex);
                    break;
                }
            }

            //any logs that weren't saved successfully get added back into the cache
            foreach (int logId in savedLogs)
            {
                EventLog log = logsToBeSaved.Where(l => l.Id == logId).FirstOrDefault();
                if (log != null)
                {
                    logsToBeSaved.Remove(log);
                }
            }

            //save the modified list back into the cache
            lock (_cache)
            {
                SaveLogsToCache(logsToBeSaved);
            }
            SendStatus.IsActive = false;
        }