:( can any one tell me how to write the errors/exceptions, occured while compiling vb.net program, in a text file?????????
IN VB.NET2003
:'(

Dani AI

Generated

The first thing to clear up: "compile" errors and "runtime" exceptions are different. Compiler errors (the red build errors you see in the IDE) are produced by the VB compiler; they are not exceptions your program can catch at runtime. To capture build output you can run the compiler from the command line and redirect its output to a file — for example vbc MyFile.vb /out:MyProg.exe > buildlog.txt 2>&1. See the VB compiler reference for details: .

For runtime exceptions (what was referring to), local Try/Catch blocks are fine for scoped error handling. To log every unhandled exception from anywhere in the app, install global handlers at startup: handle AppDomain.CurrentDomain.UnhandledException and (for WinForms) Application.ThreadException. The handlers should write a timestamp, ex.ToString() (stack plus inner exceptions) and then release the file. Example pattern (call SetupGlobalHandlers() from your startup code):

Imports System
Imports System.IO
Imports System.Windows.Forms

Module GlobalLogger
    Private ReadOnly logLock As New Object()

    Public Sub SetupGlobalHandlers()
        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException
        AddHandler Application.ThreadException, AddressOf Application_ThreadException
    End Sub

    Private Sub CurrentDomain_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs)
        Dim ex = TryCast(e.ExceptionObject, Exception)
        WriteLog(ex)
    End Sub

    Private Sub Application_ThreadException(sender As Object, e As System.Threading.ThreadExceptionEventArgs)
        WriteLog(e.Exception)
    End Sub

    Private Sub WriteLog(ex As Exception)
        If ex Is Nothing Then Return
        SyncLock logLock
            Dim folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyApp")
            If Not Directory.Exists(folder) Then Directory.CreateDirectory(folder)
            Dim path = Path.Combine(folder, "errors.log")
            Using w As New StreamWriter(path, True, System.Text.Encoding.UTF8)
                w.WriteLine(DateTime.Now.ToString("s"))
                w.WriteLine(ex.ToString())
                w.WriteLine(New String("-"c, 60))
            End Using
        End SyncLock
    End Sub
End Module

Practical tips: do not hard-code C:\ (permissions can block writes); create the folder before writing; include timestamps and full ToString() so you get stack traces and inner exceptions; guard against concurrent writes (use SyncLock or a logging library). If you want system-level recording as suggested, use System.Diagnostics.EventLog instead of a plain file: System.Diagnostics.EventLog.

If logs do not appear, run the app outside the debugger (IDE can swallow some handlers), check file ACLs, and ensure your global handlers are registered early in startup.

Recommended Answers

All 3 Replies

Why do you need this??? And can't you copy/paste the output from Visual Studio to a text file manually???

I don't have or know vb 2003 but use the Try Catch. Under the catch send the error to your log file.
Try this:

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x, y As Integer
        y = 0
        Try
            x = 5 / y
        Catch ex As Exception
            Dim writer As New StreamWriter("C:\MyLog.log", True, System.Text.Encoding.ASCII)
            writer.WriteLine(ex.Message)
            writer.Close()
        End Try

    End Sub

End Class

There is also a way you can write the exceptions to your Event Logs if that comes in handy.

If you're trying to use this to debug, you might want to use something like.

Try
blah
Catch ex as Exception
Messagebox.show(ex.tostring)
End Try

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.