Hello,
I wrote a very simple class that logs certain events coming from various places in the program by calling the AddLog method
The class includes:
-a constructor that calls the array creation method and configures a timer that fires every 2 seconds.
-method of adding an entry to the AddLog table
-a method of calling a timer that first writes records to a file and then deletes these files
And everything generally works, but from time to time there are error entries from the AddLog method as follows:
91 System.Data Object reference not set to an instance of an object
5 System.Data Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection
5 System.Data DataTable internal index is corrupted
And I don't know if there is any collision of the method that deletes records and at the same time writes to the table?
I wrote a very simple class that logs certain events coming from various places in the program by calling the AddLog method
The class includes:
-a constructor that calls the array creation method and configures a timer that fires every 2 seconds.
-method of adding an entry to the AddLog table
-a method of calling a timer that first writes records to a file and then deletes these files
And everything generally works, but from time to time there are error entries from the AddLog method as follows:
91 System.Data Object reference not set to an instance of an object
5 System.Data Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection
5 System.Data DataTable internal index is corrupted
And I don't know if there is any collision of the method that deletes records and at the same time writes to the table?
Code:
Imports System.IO
Public Class Log
Public FileWriter As StreamWriter
Private LogTablica As DataTable = New DataTable
Private SAVELOGTMR As System.Timers.Timer
Sub New()
Try
CreateLogTable()
SAVELOGTMR = New System.Timers.Timer
SAVELOGTMR.Interval = 2500
SAVELOGTMR.Stop()
AddHandler SAVELOGTMR.Elapsed, AddressOf SAVELOGTMR_Tick
SAVELOGTMR.Start()
Catch ex As Exception
End Try
End Sub
Private Sub SAVELOGTMR_Tick(ByVal source As Object, ByVal e As System.Timers.ElapsedEventArgs)
Try
SAVELOGTMR.Stop()
Dim ilosc_rekordow As Integer = 0
Dim x As Integer = 0
Dim i As Integer = 0
ilosc_rekordow = LogTablica.Rows.Count
FileWriter = New StreamWriter(".\Log.log", True)
If LogTablica.Rows.Count() <> 0 Then
'Zrzucam do pliku
For i = 0 To ilosc_rekordow - 1
FileWriter.WriteLine(CStr(LogTablica.Rows(i).Item(1)) & " " & CStr(LogTablica.Rows(i).Item(2)))
Next
FileWriter.Close()
'Usuwam rekordy zapisane
For x = 0 To ilosc_rekordow - 1
LogTablica.Rows.RemoveAt(0)
LogTablica.AcceptChanges()
Next
End If
SAVELOGTMR.Start()
Catch ex As Exception
Finally
SAVELOGTMR.Start()
End Try
End Sub
Public Sub AddLog(ByVal logitem As String)
Try
Dim Row As DataRow
Row = LogTablica.NewRow()
Row("DateTime") = Format(Now, "yyyy-MM-dd HH:mm:ss.fff")
Row("LogItem") = logitem
LogTablica.Rows.Add(Row)
LogTablica.AcceptChanges()
Catch ex As Exception
End Try
End Sub
Private Sub CreateLogTable()
Dim DateTime As DataColumn = New DataColumn("DateTime", GetType(String))
Dim LogItem As DataColumn = New DataColumn("LogItem", GetType(String))
Try
LogTablica.Columns.Add(DateTime)
LogTablica.Columns.Add(LogItem)
Catch ex As Exception
Call Error_sub_main(System.Reflection.MethodBase.GetCurrentMethod.Name.ToString)
End Try
End Sub
End Class