Tips and tricks for .NET using ASP and VB code.

Log program activity

    Public Sub LogActivity(ByVal ActivityText As String, Optional ByVal IsError As Boolean = Nothing)
        Dim TimeNow As Date = Now
        ActivityLogRichTextBox.SelectionColor = Color.DarkBlue
        ActivityLogRichTextBox.AppendText(ControlChars.CrLf & TimeNow.ToString("ddd yyyy-MM-dd hh:mm:ss.fff"))
        ActivityLogRichTextBox.SelectionColor = Color.Black
        If IsError <> Nothing Then
            If IsError = True Then
                ActivityLogRichTextBox.SelectionColor = Color.Red
            End If
        End If
        ActivityLogRichTextBox.AppendText(" " & ActivityText & ControlChars.CrLf)
        ActivityLogRichTextBox.ScrollToCaret()
        Dim LastWeeksLogFile As String = Application.ExecutablePath & ".ActivityLog." & TimeNow.AddDays(1).ToString("ddd") & ".txt"
        If My.Computer.FileSystem.FileExists(LastWeeksLogFile) Then
            My.Computer.FileSystem.DeleteFile(LastWeeksLogFile, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
        End If
        My.Computer.FileSystem.WriteAllText(Application.ExecutablePath & ".ActivityLog." & TimeNow.ToString("ddd") & ".txt", ControlChars.CrLf & TimeNow.ToString("ddd yyyy-MM-dd hh:mm:ss.fff") & " " & ActivityText & ControlChars.CrLf, True)
        Application.DoEvents()
    End Sub

No comments:

Post a Comment

Search This Blog