public override void Execute(ITaskExecutionContext <MaintenanceWorkItem> context)
        {
            ElmahConfiguration configuration = context.TypedBag <ElmahConfiguration>(ConfigurationName);

            DateTime lowerBound = DateTime.UtcNow.Date.Subtract(configuration.CleanUpEntriesOlderThan);
            int      batchSize  = configuration.BatchSize;

            using (var connection = new SqlConnection(configuration.GetConnectionString()))
                using (SqlCommand command = connection.CreateCommand())
                {
                    connection.Open();

                    command.CommandTimeout = 0;
                    command.CommandText    = DeleteSql;

                    command.Parameters.AddWithValue("time", lowerBound);
                    command.Parameters.AddWithValue("batchSize", batchSize);

                    var count = (int)command.ExecuteScalar();

                    if (count > 0)
                    {
                        context.Log.Message("Deleted {0} entries older than '{1}'.", count, lowerBound);
                    }
                }
        }
Exemple #2
0
        public override bool IsDisabled(ITaskExecutionContext context)
        {
            MonitorConfiguration configuration = _configuration.Get <MonitorConfiguration>();

            context.TypedBag(ConfigurationName, configuration);

            return(configuration.Disabled);
        }
Exemple #3
0
        public override void Execute(ITaskExecutionContext <TaskThatHasStepsWorkItem> context)
        {
            context.WorkItem.Register(this);

            ExtremelyExpensiveObject expensiveObject = context.TypedBag <ExtremelyExpensiveObject>("SomeExpensiveObject");

            context.Log.Message(Encoding.Default.GetString(expensiveObject.Data));
        }
Exemple #4
0
        public override MonitorWorkItem Start(ITaskExecutionContext context)
        {
            MonitorConfiguration configuration = context.TypedBag <MonitorConfiguration>(ConfigurationName);

            configuration.Assert();

            return(new MonitorWorkItem(configuration)
                   .AddIgnoreFilter(new MessageContainsText(configuration.IgnoreErrorsWithMessagesContaining))
                   .AddTargetRedirect(new RedirectForMonitorTargets(configuration.Targets))
                   .AddMessageGroupingPatterns(configuration.MessageGroupingPatterns));
        }
Exemple #5
0
        public static UCommerceMaintenanceConfiguration EnsureConfiguration(this ITaskExecutionContext <MaintenanceWorkItem> context, IConfigurationService configurationService)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (configurationService == null)
            {
                throw new ArgumentNullException(nameof(configurationService));
            }

            var configuration = context.TypedBag <UCommerceMaintenanceConfiguration>(Name);

            if (configuration != null)
            {
                return(configuration);
            }

            return(context.TypedBag(Name, configurationService.Get <UCommerceMaintenanceConfiguration>()));
        }
Exemple #6
0
        public override Execution ContinueWith(ITaskExecutionContext <TaskThatHasStepsWorkItem> context)
        {
            ExtremelyExpensiveObject expensiveObject = context.TypedBag("SomeExpensiveObject", new ExtremelyExpensiveObject());

            if (expensiveObject.Data.Length > 0)
            {
                return(Execution.Execute);
            }

            return(Execution.StepOut);
        }
        public override Execution ContinueWith(ITaskExecutionContext <MonitorWorkItem> context)
        {
            ElmahConfiguration configuration = _configuration.GetElmahConfiguration();

            if (configuration.GetConnectionString() == null)
            {
                return(Execution.StepOver);
            }

            if (configuration.Disabled)
            {
                return(Execution.StepOver);
            }

            context.TypedBag(ConfigurationName, configuration);

            return(Execution.Execute);
        }
        public override void Execute(ITaskExecutionContext <MonitorWorkItem> context)
        {
            ElmahConfiguration configuration = context.TypedBag <ElmahConfiguration>(ConfigurationName);

            context.WorkItem.AddMessageGroupingPatterns(MessageGroupingPattern);

            using (var connection = new SqlConnection(configuration.GetConnectionString()))
                using (SqlCommand command = connection.CreateCommand())
                {
                    connection.Open();

                    command.CommandTimeout = (int)configuration.CommandTimeout.TotalSeconds;

                    command.CommandText = @"
UPDATE [ELMAH_Error] SET 
    [AllXml] = REPLACE(CAST([AllXml] AS NVARCHAR(MAX)), '&#x', '')
WHERE [TimeUtc] BETWEEN @l AND @u;

SELECT TOP 1000
	errorId     = [ErrorId], 
	application = [Application],
    host        = [Host], 
    type        = [Type],
    source      = [Source],
    message     = [Message],
    [user]      = [User],
    statusCode  = [StatusCode], 
    time        = CONVERT(VARCHAR(50), [TimeUtc], 126) + 'Z',
	CAST([AllXml] AS XML).value('(/error/serverVariables/item[@name=''URL'']/value/@string)[1]', 'nvarchar(max)') AS url,
	CAST([AllXml] AS XML).value('(/error/serverVariables/item[@name=''QUERY_STRING'']/value/@string)[1]', 'nvarchar(max)') AS query_string,
	CAST([AllXml] AS XML).value('(/error/serverVariables/item[@name=''HTTP_REFERER'']/value/@string)[1]', 'nvarchar(max)') AS http_referer
FROM [ELMAH_Error] error 
WHERE [TimeUtc] BETWEEN @l AND @u
ORDER BY [TimeUtc] DESC, [Sequence] DESC
FOR XML AUTO";

                    command.Parameters.AddWithValue("l", context.WorkItem.CheckRange.LowerBound);
                    command.Parameters.AddWithValue("u", context.WorkItem.CheckRange.UpperBound);

                    using (XmlReader reader = command.ExecuteXmlReader())
                    {
                        try
                        {
                            int count = 0;

                            while (reader.IsStartElement("error"))
                            {
                                count++;
                                var error = new ElmahError(reader);

                                context.WorkItem.Add(
                                    error.Created,
                                    string.Join(", ", new[] { configuration.LogName, error.Source }
                                                .Where(x => !string.IsNullOrWhiteSpace(x))),
                                    error.ToString());
                            }

                            if (count > 0)
                            {
                                context.Log.Message("{0} entries within time-period {1}.", count, context.WorkItem.CheckRange);
                            }
                        }
                        catch (SqlException ex)
                        {
                            throw new InvalidOperationException(
                                      $@"{ex.Message}

---
You need to locate the row causing this error. The easiest way is to query those rows that were part of the initial selection. 
Open the XML result in SQL Server Management Studio, and find the one(s) with a red squiggles. They need to be removed.
Find out where errors like that are being generated and make sure to encode these messages, so that it won't happen again.

CommandText was: 

{command
		                        .CommandText}"        , ex);
                        }
                        catch (XmlException ex)
                        {
                            throw new InvalidOperationException(
                                      $@"Unable to parse XML. Error:

{ex.Message}

---
You need to locate the row causing this error. The easiest way is to query those rows that were part of the initial selection. 
Open the XML result in SQL Server Management Studio, and find the one(s) with a red squiggles. They need to be removed.
Find out where errors like that are being generated and make sure to encode these messages, so that it won't happen again.

CommandText was: 

{command
		                        .CommandText}"        , ex);
                        }
                    }
                }
        }
        public override MaintenanceWorkItem Start(ITaskExecutionContext context)
        {
            MaintenanceConfiguration configuration = context.TypedBag <MaintenanceConfiguration>(ConfigurationName);

            return(new MaintenanceWorkItem(configuration));
        }