overcloked Posted July 9, 2006 at 06:07 PM Report #37249 Posted July 9, 2006 at 06:07 PM Boas, estive ontem a fazer mais uma beca do projecto de gestão de espaço comercial que estou a fazer, e criei dois procedimentos que criam um Debug file em Txt, muito útil para quando o programa der erros vocês saberem o que se passou sem ter de andar a fazer debug. Objectivo: Ter um debug file que seja criado diariamente Implementação: Fácil Onde colocar: Dentro de um modulo com o nome (por exemplo) MD_Debug.vb Codigo: Private MyDebugFilePath As String = My.Application.Info.DirectoryPath & "\Debug\" & Now.Date & ".txt" 'Debug file full path must be declared inside a class 'Verify if the debug file with todays date exist, if not create one Sub CreateDebugTXT() If Not System.IO.File.Exists(MyDebugFilePath) Then Try My.Computer.FileSystem.WriteAllText(MyDebugFilePath, String.Empty, False) Catch ex As Exception Dim erro As String = "Erro ao criar Debug File" & vbCrLf & "ERRO: " & ex.ToString MsgBox(erro) Finally End Try End If End Sub 'Writte or apend new debug writteLines to the debug File Sub WritteDebug(ByVal MyError) CreateDebugTXT() Dim DefaultFileHeader As String 'Default debug header DefaultFileHeader = vbCrLf & vbCrLf & "-------------------------------------------------------------------------------" & _ vbCrLf & "Debug Created at " & Now() & _ vbCrLf & "-------------------------------------------------------------------------------" & _ vbCrLf Try My.Computer.FileSystem.WriteAllText(MyDebugFilePath, DefaultFileHeader, True) My.Computer.FileSystem.WriteAllText(MyDebugFilePath, MyError, True) Catch ex As Exception Dim erro As String = "Erro ao escrever no Debug File" & vbCrLf & "ERRO: " & ex.ToString MsgBox(erro) End Try End Sub O que estas dois procedimentos fazem: O CreateDebugTXT() primeiro verifica se o file já existe ou não caso não exista este é criado dentro da pasta da vossa aplicação\Debug\ com o nome correspondente à data de "hoje" O writteDebug() como o nome indica server para poderem escrever o erro no vosso file txt, este não escreve enquanto não verificar se o file existe para isso recorre ao procedimento anterior Como Usar: Em todo o lado onde o progrma possa dar buraco, como por exemplo tentativa de escrever numa base de dados: 'This Procedure inserts a new client in data base Public Sub InsertClient(ByVal Nome, ByVal DataNascimento, ByVal Morada, ByVal CodigoPostal, ByVal Localidade, ByVal TElefone1, ByVal Telefone2, ByVal Email, ByVal NrContribuinte) Dim Connection As OleDbConnection Dim Command As OleDbCommand Dim Query As String = "INSERT INTO CG_Clientes (N_Cliente, DataNascimento_Cliente, Morada_Cliente, CodPostal_Cliente, Localidade_Cliente, Telefone1_Cliente, Telefone2_Cliente, Email_Cliente, NrContribuinte_Cliente) " & _ "VALUES( '" & Nome & "','" & Morada & "','" & DataNascimento & "','" & CodigoPostal & "','" & Localidade & "','" & TElefone1 & "','" & Telefone2 & "','" & Email & "','" & NrContribuinte & "')" Dim DdPath As String = My.Application.Info.DirectoryPath & "\DB\CabGestBD.mdb" connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DdPath & ";User Id=admin;Password=;") Try Connection.Open() Command = New OleDbCommand(Query, Connection) Command.ExecuteNonQuery() Catch ex As Exception WritteDebug(ex.ToString) 'Aqui caso a operação não seja efectuada com o sucesso é escrito no File de Debug o porque do erro MsgBox(Erro, MsgBoxStyle.Critical) Finally connection.Close() End Try End Sub Espero que vos seja util alguma duvida é só perguntar! 🙂
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now