VB.Net Tutorial/Development/Debug

Материал из VB Эксперт
Перейти к: навигация, поиск

Convert Debug message to a file

Imports System.IO
Imports System.Diagnostics
Module Module1
    Sub Main()
        Dim Str As New FileStream("C:\DebugMessages.txt", System.IO.FileMode.Append)
        Dim DebugFile As New StreamWriter(Str)
        Dim Listener = New TextWriterTraceListener(DebugFile)
        Debug.Listeners.Add(Listener)
        Debug.AutoFlush = True
        Debug.WriteLine("")
        Debug.WriteLine("Messages generated at " & Now())
        Debug.WriteLine("In Button Click")
        Debug.WriteLine("Calling First")
        First()
        Debug.WriteLine("Exiting Button Click")
        Debug.Listeners.Remove(Listener)
        Str.Close()

    End Sub
    Private Sub First()
        Debug.WriteLine("In First")
        Debug.WriteLine("Calling Second")
        Second()
        Debug.WriteLine("Back in First")
    End Sub
    Private Sub Second()
        Debug.WriteLine("In Second")
    End Sub
End Module

Debug Status

Imports System.Diagnostics
Imports System.IO
Module Module1
    Sub Main()
        Dim Str As FileStream
        Dim Listener As TextWriterTraceListener
        If (Debugger.IsAttached) Then
            Console.WriteLine("Debugging")
            Str = New FileStream("C:\DebugMessages.txt", System.IO.FileMode.Append)
            Dim DebugFile As New StreamWriter(Str)
            Listener = New TextWriterTraceListener(DebugFile)
            Debug.Listeners.Add(Listener)
            Debug.AutoFlush = True
        Else
            Console.WriteLine("Not debugging")
        End If

        Debug.WriteLine("")
        Debug.WriteLine("Messages generated at " & Now())
        Debug.WriteLine("In Button Click")
        Debug.WriteLine("Calling First")
        First()
        Debug.WriteLine("Exiting Button Click")
        If (Debugger.IsAttached) Then
            Debug.Listeners.Remove(Listener)
            Str.Close()
            Console.WriteLine("Done")
        End If
    
    End Sub
    Private Sub First()
        Debug.WriteLine("In First")
        Debug.WriteLine("Calling Second")
        Second()
        Debug.WriteLine("Back in First")
    End Sub
    Private Sub Second()
        Debug.WriteLine("In Second")
    End Sub
End Module
Not debugging

Use Debug.Assert

Imports System.IO
Module Module1
    Sub Main()
        Dim FileStr As StreamWriter
        System.Diagnostics.Debug.Assert(Not (FileStr Is Nothing), "FileStr is NULL")
        System.Diagnostics.Debug.Assert(Directory.Exists("\Temp"), "\Temp does not exist")
    End Sub
End Module