Click to See Complete Forum and Search --> : how do i set a record of errors?
area91
Nov 11th, 2002, 04:09 PM
Ok, in my VB.NET APP its very buggy right now...how do i creat a think that writes to a text file every time there is a error..kinda like this :
ERR_******* DATE
please help? and any time there is a new error puts the info on the next blank line
area91
Nov 12th, 2002, 05:12 PM
HeLp
Pirate
Nov 12th, 2002, 05:33 PM
as error log file
simply ,
declare a variable
then try to catch(handle) any expected error , pass it to the variable .
open file and append the variable .
close the file
Edneeis
Nov 12th, 2002, 05:35 PM
Here is what I usually use:
Public Class ErrorHelpers
Public Shared Function getErrorFilename() As String
Dim asmy As System.Reflection.Assembly
Dim tempName As String
asmy = System.Reflection.Assembly.GetEntryAssembly()
tempName = asmy.Location
Return tempName & ".log"
End Function
Public Shared Sub LogError(ByVal e As Exception)
Dim fs As New IO.FileStream(getErrorFilename, IO.FileMode.Append, IO.FileAccess.Write)
Dim sw As New IO.StreamWriter(fs)
sw.WriteLine("--------------------------------------------------------")
sw.WriteLine(String.Concat(TypeName(e), " occurred ", DateTime.Now))
sw.WriteLine(String.Concat("Error : ", e.Message))
sw.WriteLine(String.Concat("Source : ", e.Source))
sw.WriteLine(String.Concat("Target : ", e.TargetSite))
sw.WriteLine(String.Concat("Stack : ", e.StackTrace))
sw.Flush()
sw.Close()
fs.Close()
sw = Nothing
fs = Nothing
End Sub
End Class
You'd use it like this:
ErrorHelpers.LogError(ex)
Either in your Try Catch block or you can probably grab all errors with the Application.ThreadException event.
Musician
Nov 12th, 2002, 08:10 PM
The .Net way (and of course there is one) is to use a trace listener to store debug messages.
First you create a Trace Listener object and tell it what you want to do, in this case write to a file so we give it a filestream object:-
Dim myTraceLog as New System.IO.FileStream("C:\myTraceLog.txt", IO.FileMode.OpenOrCreate)
' Creates the new trace listener
Dim myListener As New TextWriterTraceListener(myTraceLog)
Now add the listener to the trace.listeners collection:-
Trace.Listeners.Add(myListener)
You don't have to do this - you can specifically use the listener object to output log messages to your file :-
myListener.WriteLine("This message does not use the listener collection but is written to my log file")
Now you can use the shared procedures of the trace or debug object to send messages to your trace listener. In this case one tracewriter (could be other listener objects in the collection).
Debug.Write("Error in procedure line5 etc. yada yada")
'or Debug.WriteLine("Another bug - for crying out loud!")
'or try some nice indentation for easy reading
Debug.WriteLine("Staring my custom procedure")
Debug.Indent
Debug.WriteLine("Processed first section")
Debug.WriteLine("Finished custom procedure")
Debug.Unindent
Debug.Writeline("Starting second procedure")
etc.
by the way got the code examples from the .net docs so dont worry I'm not taking credit.
Edneeis
Nov 12th, 2002, 11:30 PM
Hey thats cool Musician, thanks!
Edneeis
Nov 13th, 2002, 11:22 AM
To get it to work with the Debug statements I had to set AutoFlush to true (just in case anyone else tries).
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.