In this program whenever error is generated then all the details of the error will be saved in the text file.(similar to log files in windows).
Imports System.IO Imports System.Text Imports System Public Class CError 'FileStream deals with the handling of files. Its supports both reading as well as writing in a file. Dim fs As FileStream Public Sub createfile(ByVal path As String, ByVal errormsg As String)
'File.Exists(path) checks whether file exit or not it exists then return true else return false. If File.Exists(path) = False Then 'File.Create(path) will create the file in specified path Dim fs As FileStream = File.Create(path) 'UTF8Encoding(True) means Initializes a new instance of the UTF8Encoding class. A parameter specifies whether to prefix an encoding with a Unicode byte order mark. 'UTF8Encoding.GetBytes Encodes the characters from a specified String and returns the results in a byte array. Dim info As Byte() = New UTF8Encoding(True).GetBytes(errormsg) 'fs.Write(Array() as byte,From where to start for writing in file,how many characters should be write in a file as integer) fs.Write(info, 0, info.Length) 'fs.close() will close the file. fs.Close()
Else 'Fs open the existing file and append the error message inside that. Dim fs As FileStream = File.Open(path, FileMode.Append) Dim info As Byte() = New UTF8Encoding(True).GetBytes(errormsg) fs.Write(info, 0, info.Length) 'fs.close() will close the file. fs.Close() End If End Sub End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try Dim con As OleDb.OleDbConnection
TextBox1.Text = "E:\Neet Full Data\NEETU (F)\VBNet\Net class\Error Handling\ErrorHNDLING\erreornew.txt" con.ConnectionString = "" con.Open() Catch ex As Exception Dim filename As New StringBuilder filename.Append("E:\Neet Full Data\NEETU (F)\VBNet\Net class\Error Handling\ErrorHNDLING\erreornew.txt") 'filename.Append(Date.Today.Date.ToString) 'filename.Append(".txt") MsgBox(filename.ToString) Dim str As String = ex.Message + " TIME & DATE " + Date.Now obj.createfile(filename.ToString, str)
End Try
End Sub
regards Neetu
AttachmentsCreating log files of an error. (30991-3610-ErrorHNDLING.rar)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|