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
Printable View
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
HeLp
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
Here is what I usually use:
VB Code:
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.
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:-
VB Code:
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:-
VB Code:
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 :-
VB Code:
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).
VB Code:
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.
Hey thats cool Musician, thanks!
To get it to work with the Debug statements I had to set AutoFlush to true (just in case anyone else tries).