Beispiel #1
0
        public List<Models.LogLiveEvent> Query(DiscoDataContext Database)
        {
            List<Models.LogLiveEvent> results = new List<LogLiveEvent>();

            // Validate Options
            this.Validate();

            var relevantLogFiles = RelevantLogFiles(Database);
            relevantLogFiles.Reverse();
            foreach (var logFile in relevantLogFiles)
            {
                SqlCeConnectionStringBuilder sqlCeCSB = new SqlCeConnectionStringBuilder();
                sqlCeCSB.DataSource = logFile.Item1;

                var logModules = LogContext.LogModules;

                using (var context = new LogPersistContext(sqlCeCSB.ToString()))
                {
                    var query = this.BuildQuery(context, logFile.Item2, results.Count);
                    IEnumerable<LogEvent> queryResults = query; // Run the Query
                    results.AddRange(queryResults.Select(le => Models.LogLiveEvent.Create(logModules[le.ModuleId], logModules[le.ModuleId].EventTypes[le.EventTypeId], le.Timestamp, le.Arguments)));
                }
                if (this.Take.HasValue && this.Take.Value < results.Count)
                    break;
            }
            return results;
        }
Beispiel #2
0
 public void Log(int ModuleId, int EventTypeId, params object[] Args)
 {
     LogBase logModule;
     if (LogModules.TryGetValue(ModuleId, out logModule))
     {
         Models.LogEventType eventType;
         if (logModule.EventTypes.TryGetValue(EventTypeId, out eventType))
         {
             var eventTimestamp = DateTime.Now;
             if (eventType.UseLive)
             {
                 LogNotificationsHub.BroadcastLog(logModule, eventType, eventTimestamp, Args);
             }
             if (eventType.UsePersist)
             {
                 string args = null;
                 if (Args != null && Args.Length > 0)
                 {
                     args = JsonConvert.SerializeObject(Args);
                 }
                 using (var context = new Persistance.LogPersistContext(PersistantStoreConnectionString))
                 {
                     var e = new Models.LogEvent()
                     {
                         Timestamp = eventTimestamp,
                         ModuleId = logModule.ModuleId,
                         EventTypeId = eventType.Id,
                         Arguments = args
                     };
                     context.Events.Add(e);
                     context.SaveChanges();
                 }
             }
         }
         else
             throw new InvalidOperationException(string.Format("Unknown Log Event Type Called: {0} (for Module: {1})", EventTypeId, ModuleId));
     }
     else
         throw new InvalidOperationException(string.Format("Unknown Log Module Called: {0}", ModuleId));
 }
Beispiel #3
0
 private IQueryable<LogEvent> BuildQuery(LogPersistContext LogContext, DateTime LogDate, int Taken)
 {
     IQueryable<LogEvent> query = LogContext.Events.OrderByDescending(le => le.Timestamp);
     if (this.Module.HasValue)
     {
         query = query.Where(le => le.ModuleId == this.Module.Value);
     }
     if (this.EventTypes != null && this.EventTypes.Count > 0)
     {
         query = query.Where(le => this.EventTypes.Contains(le.EventTypeId));
     }
     if (this.Start.HasValue && this.Start.Value > LogDate)
     {
         var startValue = DateTime.SpecifyKind(this.Start.Value, DateTimeKind.Local);
         query = query.Where(le => le.Timestamp > startValue);
     }
     if (this.End.HasValue && this.End.Value <= LogDate.AddDays(1))
     {
         var endValue = DateTime.SpecifyKind(this.End.Value, DateTimeKind.Local);
         query = query.Where(le => le.Timestamp < endValue);
     }
     if (this.Take.HasValue && this.Take.Value > 0)
     {
         var take = this.Take.Value - Taken;
         query = query.Take(take);
     }
     return query;
 }
Beispiel #4
0
        internal static void ReInitalize(DiscoDataContext Database)
        {
            lock (_CurrentLock)
            {
                var logPath = LogFilePath(Database, DateTime.Today);

                //var connectionString = string.Format("Data Source=\"{0}\"", logPath);

                SqlCeConnectionStringBuilder sqlCeCSB = new SqlCeConnectionStringBuilder();
                sqlCeCSB.DataSource = logPath;
                var connectionString = sqlCeCSB.ToString();

                // Ensure Database Exists
                if (!File.Exists(logPath))
                {
                    // Create Database
                    using (var context = new Persistance.LogPersistContext(connectionString))
                    {
                        context.Database.CreateIfNotExists();
                    }
                }

                // Add Modules/Event Types
                InitalizeModules();
                using (var context = new Persistance.LogPersistContext(connectionString))
                {
                    InitalizeDatabase(context);
                }

                // Create Current LogContext
                var currentLogContext = new LogContext(logPath, connectionString);
                _Current = currentLogContext;
            }
            SystemLog.LogLogInitialized(_Current.PersistantStorePath);
            try
            {
                // Get Yesterdays Log
                var yesterdaysLogPath = LogFilePath(Database, DateTime.Today.AddDays(-1), false);
                if (File.Exists(yesterdaysLogPath))
                {
                    SqlCeConnectionStringBuilder sqlCeCSB = new SqlCeConnectionStringBuilder();
                    sqlCeCSB.DataSource = yesterdaysLogPath;
                    var connectionString = sqlCeCSB.ToString();
                    int logCount;
                    using (var context = new Persistance.LogPersistContext(connectionString))
                    {
                        logCount = context.Events.Where(e => !(e.ModuleId == 0 && e.EventTypeId == 100)).Count();
                        if (logCount == 0)
                        {
                            // Delete (empty) Database
                            context.Database.Delete();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                SystemLog.LogError("Error occurred while investigating yesterdays log for deletion", ex.GetType().Name, ex.Message, ex.StackTrace);
            }
        }