Beispiel #1
0
        private void MarkRunInternalError(IRunState runState, Exception ex)
        {
            using (new SecurityBypassContext())
            {
                var safeDescription = GetSafeWfDescription(runState);
                var stateString     = GetStateString(runState);

                var logEntry = new WorkflowRunLogEntry
                {
                    Name        = "Workflow run internal error",
                    Description = string.Format("Workflow failed with an internal error at {0}.\nState: {1}", safeDescription, stateString)
                };


                var msg = string.Format("Workflow run ({0}): {1}. Exception occurred on run.\n{2}\n{3}\nState:\n{4}",
                                        runState != null ? runState.WorkflowRunId : -1L,
                                        safeDescription,
                                        ex.Message,
                                        ex.StackTrace,
                                        stateString);

                EventLog.Application.WriteError(msg);

                if (runState != null)
                {
                    runState.RunStatus = WorkflowRunState_Enumeration.WorkflowRunFailed;
                    runState.LogError(logEntry);
                }
            }
        }
        public void GetEventLogEntitiesToDelete(int numEventLogEntries, int maxEventLogEntries)
        {
            IList <WorkflowRunLogEntry> entries;
            ICollection <long>          entitiesToDelete;

            // Remove any existing log entries
            Entity.Delete(Entity.GetInstancesOfType <WorkflowRunLogEntry>().Select(e => e.Id));

            // Create the test entries
            entries = Enumerable.Range(0, numEventLogEntries)
                      .Select(i =>
            {
                WorkflowRunLogEntry entry;

                entry = new WorkflowRunLogEntry
                {
                    Name = string.Format("Test Entry {0}", i + 1),
                    LogEntrySeverity_Enum = LogSeverityEnum_Enumeration.InformationSeverity
                };
                entry.Save();

                // Wait to ensure creation times are different. The resolution of
                // CreatedDate is a second, unfortunately.
                Thread.Sleep(1000);

                return(entry);
            })
                      .ToList();

            // Reload the entities to get the correct created date and other fields
            entries = Entity.Get <WorkflowRunLogEntry>(entries.Select(e => e.Id), new IEntityRef[] { Resource.CreatedDate_Field })
                      .OrderBy(e => e.CreatedDate)
                      .ToList();

            // Find the entities to delete
            entitiesToDelete = new ActivityLogPurger().GetEventLogEntitiesToDelete(maxEventLogEntries);

            Assert.That(
                entitiesToDelete,
                Is.EquivalentTo(
                    entries.Take(Math.Max(numEventLogEntries - maxEventLogEntries, 0))
                    .Select(e => e.Id)));
        }
        public void Purge(int numEventLogEntries, int maxEventLogEntries)
        {
            IList <WorkflowRunLogEntry> entries;
            EventLogSettings            eventLogSettings;
            int numberEntitiesDeleted;

            var workflowRunLogEntries = Entity.GetInstancesOfType <WorkflowRunLogEntry>( ).ToList( );

            var longs = workflowRunLogEntries.Select(e => e.Id).ToList( );

            // Remove any existing log entries
            Entity.Delete(longs);

            // Create the test entries
            entries = Enumerable.Range(0, numEventLogEntries)
                      .Select(i =>
            {
                WorkflowRunLogEntry entry;

                entry = new WorkflowRunLogEntry
                {
                    Name = string.Format("Test Entry {0}", i + 1),
                    LogEntrySeverity_Enum = LogSeverityEnum_Enumeration.InformationSeverity
                };
                entry.Save();

                // Wait to ensure creation times are different. The resolution of
                // CreatedDate is a second, unfortunately.
                Thread.Sleep(1000);

                return(entry);
            })
                      .ToList();

            // Reload the entities to get the correct created date and other fields
            entries = Entity.Get <WorkflowRunLogEntry>(entries.Select(e => e.Id), new IEntityRef[] { Resource.CreatedDate_Field })
                      .OrderBy(e => e.CreatedDate)
                      .ToList();

            // Set the maximum number of event log entries
            eventLogSettings = Entity.Get <EventLogSettings>(ActivityLogPurger.EventLogSettingsAlias, true);
            eventLogSettings.MaxEventLogEntries = maxEventLogEntries;
            eventLogSettings.Save();

            using (EventLogMonitor eventLogMonitor = new EventLogMonitor())
            {
                new ActivityLogPurger().Purge();

                numberEntitiesDeleted = Math.Max(numEventLogEntries - maxEventLogEntries, 0);
                if (numberEntitiesDeleted > 0)
                {
                    Assert.That(
                        eventLogMonitor.Entries,
                        Has.Exactly(1)
                        .Property("Message").Matches("Deleting \\d+ excess event log entry entities.")
                        .And.Property("Level").EqualTo(EventLogLevel.Information));
                }

                workflowRunLogEntries = Entity.GetInstancesOfType <WorkflowRunLogEntry>( ).ToList( );

                longs = workflowRunLogEntries.Select(e => e.Id).ToList( );

                Assert.That(longs,
                            Is.EquivalentTo(
                                entries.Reverse()
                                .Take(Math.Min(maxEventLogEntries, numEventLogEntries))
                                .Select(e => e.Id)));
            }
        }