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);
                    }
                }
        }
        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);
                        }
                    }
                }
        }