Author Topic: Add logging to MessageDisplayByValue or other similar controls  (Read 2605 times)

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
There comes a time that one might need to log values or simply recording alarm messages.
The original code is from Archie, I just corrected a few minor errors and put it here for the convenience of searching

1. Close out all open forms
2. Right click AdvancedHMIControls\PurchasedControls folder and Add Existing Item
   Select MessageDisplayByValue.vb from the Controls folder

3. Rename the file to MessageDisplayByValueWithLogging.vb and answer NO to the dialog box

4. Open & Edit the line to: Public Class MessageDisplayByValueWithLogging

5. Add 2 new properties into the Properties Region
Code: [Select]
    Private m_LogFile As String = ""
    Public Property LogFile As String
        Get
            Return m_LogFile
        End Get
        Set(value As String)
            m_LogFile = value
        End Set
    End Property

    Private m_DaysToKeep As Integer = 10
    Public Property LogFileDaysToKeep As Integer
        Get
            Return m_DaysToKeep
        End Get
        Set(value As Integer)
            m_DaysToKeep = value
        End Set
    End Property

6. And Add an event into the Event Region
Code: [Select]
Protected Overrides Sub OnTextChanged(e As System.EventArgs)
        MyBase.OnTextChanged(e)

        If m_LogFile <> "" Then
            System.IO.Directory.CreateDirectory(m_LogFile)
            If Me.Text <> "" Then
                Dim sw As New System.IO.StreamWriter(m_LogFile & "\" & m_LogFile & Format(Now(), "yyyyMMdd") & ".txt", True)
                sw.WriteLine(DateString & "     " & TimeOfDay & "     " & MyBase.Text)
                sw.Close()
            End If

            For Each file As IO.FileInfo In New IO.DirectoryInfo(LogFile).GetFiles("*.txt")
                If (Now - file.CreationTime).Days >= m_DaysToKeep Then file.Delete()
            Next

        End If
End Sub

7.  Build your solution
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================