Ejemplo n.º 1
0
 private void Flush()
 {
     if (_flushing)
     {
         return;
     }
     if (!_app.IsConnected())
     {
         return;
     }
     lock (_lock) {
         try {
             _flushing = true;
             var start = _app.TimeService.ElapsedMilliseconds;
             var list  = DequeuAll();
             if (list.Count == 0)
             {
                 return;
             }
             //fire event
             Saving?.Invoke(this, new BackgroundSaveEventArgs(list));
             var session = (EntitySession)_app.OpenSystemSession();
             session.LogDisabled = true; //do not log operations; what we save are log entries themselves most of the time
             if (list.Count > 0)
             {
                 var byHandler = list.GroupBy(o => GetHandler(o));
                 foreach (var group in byHandler)
                 {
                     var handler = group.Key;
                     if (handler == null)
                     {
                         continue;
                     }
                     handler.SaveObjects(session, group.ToList());
                 }
             }
             //Get stats
             var recCount    = session.GetChangeCount();
             var startSaving = _app.TimeService.ElapsedMilliseconds;
             session.SaveChanges();
             var endSaving = _app.TimeService.ElapsedMilliseconds;
             //log stats
             if (_operationLog != null && OkToLogStatsMessage(list))
             {
                 var logEntry = new Logging.InfoLogEntry(session.Context, "Background save completed, records: {0}, save time: {1} ms, total time: {2} ms.",
                                                         recCount, (endSaving - startSaving), (endSaving - start));
                 _operationLog.Log(logEntry);
             }
         } catch (Exception ex) {
             //System.Diagnostics.Debugger.Break();
             _errorLog.LogError(ex);
         } finally {
             _flushing = false;
         }
     }
 }//method