Forum Discussion

100Rub's avatar
100Rub
New Contributor III
8 months ago

Multiple error log deletion

Hi,

Can somebody please tell me the process/code of clearing multiple errors let's say 12 pages from Error Log in one go? There is a limitation in Administrator Solution Tools to delete logs prior to 30 days. I would really appreciate the help.

Thanks

 

  • MarcusH's avatar
    MarcusH
    Contributor III

    I can't see any limitation in the code - I have attached it in case it answers your question:

    Private Sub ClearErrorLog(ByVal si As SessionInfo, retentionDays As Integer, stepThreshold As Integer, taskItem As TaskActivityItem, batchSize As Integer, clearLogUser As String, clearLogApplication As String, errorLogDescription As String)
        Try
            'Initialize the Task Activity update threshold
            Dim updateTime As DateTime = DateTime.Now.AddMinutes(5)
            'Calculate the date threshold to used for the delete criteria
            Dim yearMonthDay As String = GetThresholdDate(si, retentionDays, stepThreshold)
            'Make sure that we do not clear log items if this Step Threshold is less than the Threshold selected by the user
            If (Not String.IsNullOrEmpty(yearMonthDay)) Then
    
                Dim parameters As New List(Of DbParamInfo) From {
                        New DbParamInfo("@yearMonthDay", yearMonthDay),
                        New DbParamInfo("@clearLogUser", clearLogUser),
                        New DbParamInfo("@clearLogApplication", clearLogApplication),
                        New DbParamInfo("@errorLogDescription", errorLogDescription.Trim())
                        }
    
                'Delete all log information older than the supplied date
                Dim msg As New Text.StringBuilder
                msg.Append($"Delete Error Log ({yearMonthDay}) ")
    
                Using dbConnFW As DbConnInfo = BRApi.Database.CreateFrameworkDbConnInfo(si)
                    Dim logCount As Long = 0
    
                    'Get count of errors that match this date criteria and store in logCount variable
                    Dim sqlErrorLog As String = "SELECT COUNT(*) AS ErrorCount
                    FROM ErrorLog
                    WHERE (ErrorTime < CONVERT(DATETIME, @yearMonthDay, 102))"
                    If clearLogUser <> "(All)" Then
                        sqlErrorLog &= " AND UserName = @clearLogUser"
                    End If
                    If clearLogApplication <> "(All)" Then
                        sqlErrorLog &= " AND AppName = @clearLogApplication"
                    End If
                    If Not String.IsNullOrWhiteSpace(errorLogDescription.Trim) Then
                        sqlErrorLog &= " AND Description LIKE '%' + @errorLogDescription + '%' "
                    End If
                    Using dt As DataTable = BRApi.Database.ExecuteSql(dbConnFW, sqlErrorLog, parameters, True)
                        logCount = CType(dt.Rows(0).Item(0), Long)
                    End Using
    
                    'Now delete all of the Error Log Entries one batch at a time
                    Dim sqlLogDelete As String = $"DELETE TOP ({batchSize}) 
                    FROM ErrorLog
                    WHERE (ErrorTime < CONVERT(DATETIME, @yearMonthDay, 102))"
                     If clearLogUser <> "(All)" Then
                        sqlLogDelete &= " AND UserName = @clearLogUser"
                    End If
                    If clearLogApplication <> "(All)" Then
                        sqlLogDelete &= " AND AppName = @clearLogApplication"
                    End If
                    If errorLogDescription.Trim <> "" Then
                        sqlLogDelete &= " AND Description LIKE '%' + @errorLogDescription + '%' "
                    End If
                    'Now keep executing and committing this statement until the records affected count = 0
                    Dim cumErrorDeleteCount As Long = 0
                    Dim rowsErrorDeleted As Long = 0
                    Dim pctComplete As Decimal = 0d
    
                    'Keep deleting as long as the number of rows is equal to the batch size
                    Do While cumErrorDeleteCount < logCount
                        'Delete rows, but limit to batch size
                        rowsErrorDeleted = DbSql.ExecuteActionQuery(dbConnFW, CommandType.Text, sqlLogDelete.ToString, parameters, True)
                        cumErrorDeleteCount += rowsErrorDeleted
    
                        'Update TaskActivityStatus
                        If taskItem IsNot Nothing Then
                            If DateTime.Now >= updateTime Then
                                'Reset the update time indicator
                                updateTime = DateTime.Now.AddMinutes(5)
                                'Update the task status
                                pctComplete = Math.Round(((cumErrorDeleteCount / logCount) * 100), 0)
                                UpdateTaskActivityStatus(si, taskItem, $"Deleted: ({cumErrorDeleteCount}) Errors Of ({logCount}) Total Errors", pctComplete, 100)
                            End If
                        End If
                    Loop
    
                    'Update task status for final delete (Set to 100%)
                    UpdateTaskActivityStatus(si, taskItem, "Deleting Error Log", 100, 100)
    
                    'Log the delete information
                    msg.Append($"Error Log Entries = {logCount}")
                    If logCount > 0 Then
                        BRApi.ErrorLog.LogMessage(si, msg.ToString)
                    End If
                End Using
            End If
    
        Catch ex As Exception
            Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
        End Try
    End Sub
    

     

    • 100Rub's avatar
      100Rub
      New Contributor III

      Hi MarcusH, 

      Thanks for your answer. I am not a coder but  as I can see in the dashboard of Administrator Solution Tools, Days to Retain is still the same i.e. 30 days by applying your code in the AST_ClearLogsForThreshold extender. I would appreciate if this can be reduced to zero.

       

      Thanks

      • MarcusH's avatar
        MarcusH
        Contributor III

        I have found where the limit is. You will need to update the function GetThreshold limit in the Extender rule AST_ClearLogsForThreshold like this:

        Private Function GetThresholdDate(ByVal si As SessionInfo, retentionDays As Integer, stepThreshold As Integer) As String
            Try
                Dim yearMonthDay As String = String.Empty
        
                'Make sure that we do not clear log items if this Step Threshold is less than the Threshold selected by the user
                'and do NOT allow retention days less than 30.
                ' Remove the 30 day limit
                ' If (retentionDays <> 0) AndAlso (retentionDays >= 30) AndAlso (stepThreshold <> 0) AndAlso (stepThreshold >= retentionDays) Then
                If (retentionDays <> 0) AndAlso (stepThreshold <> 0) AndAlso (stepThreshold >= retentionDays) Then
                    'Calculate the date threshold to used for the delete criteria
                    yearMonthDay = DateTime.Now().AddDays(stepThreshold * -1).ToString("yyyy-MM-dd 00:00:00")
                End If
                Return yearMonthDay
        
            Catch ex As Exception
                Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
            End Try
        End Function

        And secondly update the parameter RetentionDays_ASTLCR so that days less than 30 can be entered. There must have been a reason why the 30 day limit was introduced so you should test this thoroughly before clearing it all. There is still a restriction on entering 0 days.