Click here to Skip to main content
15,886,873 members
Articles / Programming Languages / Visual Basic

Text Log Viewer

Rate me:
Please Sign up or sign in to vote.
3.30/5 (6 votes)
19 Aug 2009CPOL2 min read 53.3K   2.3K   30  
Text Log Viewer
Public Class Frm_WEvents

#Region "On Tanimlar"

    Dim Takip As System.Threading.Thread
    Dim Cls_Aktif As Boolean = False
    Dim Cls_ServerIsmi As String = String.Empty
    Dim Cls_Logismi As String = String.Empty
#End Region

#Region "Form Olaylari"

    Private Sub Frm_WEvents_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Cls_Aktif = False
    End Sub


    Private Sub Frm_WEvents_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        LoglariBul(Txt_Comp.Text)

        For i As Int16 = 0 To Grd_Log.Columns.Count - 1
            Grd_Log.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
        Next
        Grd_Log.Columns("Bilgi").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        LoglariBul()

    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        Cls_Aktif = CheckBox1.Checked

        Txt_Ts.Enabled = Not (CheckBox1.Checked)
        Cmb_ELog.Enabled = Not (CheckBox1.Checked)
        Txt_Comp.Enabled = Not (CheckBox1.Checked)
        If Cls_Aktif Then

            Baslat()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        LogGoster(Txt_Comp.Text, Cmb_ELog.Text)

    End Sub

#End Region


#Region "Thereat"

    Sub T_Takip()
        Dim log As New WindowsEventLogReader
        Dim sondanbironceki As New C_EventLog
        Do
            Dim son As New C_EventLog
            son = log.SonLogBilgisi(Cls_Logismi, Cls_ServerIsmi)

            If son IsNot Nothing Then
                If son.Zaman <> sondanbironceki.Zaman And son.Mesaj <> sondanbironceki.Mesaj And son.Kaynak <> sondanbironceki.Kaynak Then
                    sondanbironceki.Zaman = son.Zaman
                    sondanbironceki.Kaynak = son.Kaynak
                    sondanbironceki.Tipi = son.Tipi
                    sondanbironceki.Mesaj = son.Mesaj
                    Ekle(son)
                End If
            End If

            System.Threading.Thread.Sleep(Txt_Ts.Text)
        Loop While Cls_Aktif



    End Sub

#End Region

#Region "Sub"

    Sub Baslat()
        Takip = New System.Threading.Thread(AddressOf T_Takip)
        Cls_Aktif = True
        Cls_ServerIsmi = Txt_Comp.Text
        Cls_Logismi = Cmb_ELog.Text
        Takip.Start()
    End Sub


    Sub LoglariBul(Optional ByVal serverismi As String = "")
        Try
            Cmb_ELog.Items.Clear()
            Dim E_list As New List(Of C_LogIsimleri)
            Dim log As New WindowsEventLogReader
            If Txt_Comp.Text <> "local" Then
                E_list = log.LogKayitIsimleriniBul(Txt_Comp.Text)
            Else

                E_list = log.LogKayitIsimleriniBul()
            End If


            For Each c As C_LogIsimleri In E_list
                Cmb_ELog.Items.Add(c.Logismi)
            Next
        Catch ex As Exception
            MsgBox("Event Log Isimlerine UIasilamiyor.Hata : " & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub

    Sub LogGoster(ByVal _server As String, ByVal _Logname As String)
        Dim log As New WindowsEventLogReader
        Dim a As New List(Of C_EventLog)

        a = log.ListEventLog(_Logname, _server)

        For i As Long = a.Count - 1 To a.Count - 100 Step -1
            Try
                Ekle(a(i))
            Catch ex As Exception

            End Try

        Next

        
    End Sub


    Delegate Sub Ekle1(ByRef a As C_EventLog)

    Sub Ekle(ByRef a As C_EventLog)
        If Grd_Log.InvokeRequired Then
            Dim d As New Ekle1(AddressOf Ekle)
            Grd_Log.Invoke(d, New Object() {a})
        Else

            Dim r1 As New DataGridViewRow

            Dim c1 As New DataGridViewTextBoxCell
            Dim c2 As New DataGridViewImageCell
            Dim c3 As New DataGridViewTextBoxCell
            Dim c4 As New DataGridViewTextBoxCell
            Dim c5 As New DataGridViewTextBoxCell

            CellStil(c1, New Cls_SatirOZellik("", Color.Black, Color.White, 0)) : CellStil(c3, New Cls_SatirOZellik("", Color.Black, Color.White, 0)) : CellStil(c4, New Cls_SatirOZellik("", Color.Black, Color.White, 0))
            '  If yazistil.ImgIndex <> -1 Then


            If a.Tipi = 4 Then 'info
                c2.Value = MainF.img1.Images(31)
            ElseIf a.Tipi = 1 Then ' error
                c2.Value = MainF.img1.Images(4)
            Else
                c2.Value = MainF.img1.Images(3) 'excla
            End If

            r1.Cells.Add(c1)
            r1.Cells.Add(c2)
            r1.Cells.Add(c3)
            r1.Cells.Add(c4)
            r1.Cells.Add(c5)

            Try
                r1.Cells(0).Value = Grd_Log.Rows(Grd_Log.Rows.Count - 1).Cells(0).Value + 1
            Catch ex As Exception
                Dim say As Long = 0 ' _ssayisi - Txt_MAxLog.Text + 1
                If say <= 0 Then say = 1
                r1.Cells(0).Value = say
            End Try

            r1.Cells(2).Value = a.Kaynak
            r1.Cells(3).Value = a.Zaman
            r1.Cells(4).Value = a.Mesaj

            Grd_Log.Rows.Add(r1)

        End If

    End Sub
#End Region
   


   
    Private Sub Grd_Log_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Grd_Log.CellContentClick

    End Sub

    Private Sub Grd_Log_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles Grd_Log.RowsAdded
        On Error Resume Next
        If Grd_Log.Rows.Count > 300 Then
            Grd_Log.Rows.RemoveAt(0)
        End If
        Grd_Log.Rows(Grd_Log.Rows.Count - 2).Selected = False
        Grd_Log.Rows(Grd_Log.Rows.Count - 1).Selected = True

        Grd_Log.FirstDisplayedScrollingRowIndex = Grd_Log.RowCount - 1
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions