private void FlushCache(bool isDisposed = false)
        {
            List <EventMessage> events = null;

            if (DateTime.UtcNow > lastSave.Add(CacheTime) || Cache.Count > CacheLimit || isDisposed)
            {
                lock (Cache)
                {
                    Timer.Change(-1, -1);
                    timerStarted = false;

                    events = new List <EventMessage>(Cache.Values);
                    Cache.Clear();
                    lastSave = DateTime.UtcNow;
                }
            }

            if (events == null)
            {
                return;
            }

            using (var db = DbManager.FromHttpContext(MessagesDbId))
                using (var tx = db.BeginTransaction(IsolationLevel.ReadUncommitted))
                {
                    var dict = new Dictionary <string, ClientInfo>();

                    foreach (var message in events)
                    {
                        if (!string.IsNullOrEmpty(message.UAHeader))
                        {
                            try
                            {
                                MessageSettings.AddInfoMessage(message, dict);
                            }
                            catch (Exception e)
                            {
                                LogManager.GetLogger("ASC").Error("FlushCache " + message.Id, e);
                            }
                        }

                        if (!ForseSave(message))
                        {
                            // messages with action code < 2000 are related to login-history
                            if ((int)message.Action < 2000)
                            {
                                AddLoginEvent(message, db);
                            }
                            else
                            {
                                AddAuditEvent(message, db);
                            }
                        }
                    }

                    tx.Commit();
                }
        }
        public int Add(EventMessage message)
        {
            if (ForseSave(message))
            {
                int id = 0;
                if (!string.IsNullOrEmpty(message.UAHeader))
                {
                    try
                    {
                        MessageSettings.AddInfoMessage(message);
                    }
                    catch (Exception e)
                    {
                        LogManager.GetLogger("ASC").Error("Add " + message.Id, e);
                    }
                }
                using (var db = DbManager.FromHttpContext(MessagesDbId))
                {
                    if ((int)message.Action < 2000)
                    {
                        id = AddLoginEvent(message, db);
                    }
                    else
                    {
                        id = AddAuditEvent(message, db);
                    }
                }
                return(id);
            }

            var now = DateTime.UtcNow;
            var key = string.Format("{0}|{1}|{2}|{3}", message.TenantId, message.UserId, message.Id, now.Ticks);

            lock (Cache)
            {
                Cache[key] = message;

                if (!timerStarted)
                {
                    Timer.Change(0, 100);
                    timerStarted = true;
                }
            }
            return(0);
        }