Results 1 to 7 of 7

Thread: how do i set a record of errors?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Toronto, Ontario, Canada
    Posts
    275

    how do i set a record of errors?

    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

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Toronto, Ontario, Canada
    Posts
    275
    HeLp

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is what I usually use:

    VB Code:
    1. Public Class ErrorHelpers
    2.  
    3.     Public Shared Function getErrorFilename() As String
    4.         Dim asmy As System.Reflection.Assembly
    5.         Dim tempName As String
    6.  
    7.         asmy = System.Reflection.Assembly.GetEntryAssembly()
    8.         tempName = asmy.Location
    9.  
    10.         Return tempName & ".log"
    11.     End Function
    12.  
    13.     Public Shared Sub LogError(ByVal e As Exception)
    14.         Dim fs As New IO.FileStream(getErrorFilename, IO.FileMode.Append, IO.FileAccess.Write)
    15.         Dim sw As New IO.StreamWriter(fs)
    16.         sw.WriteLine("--------------------------------------------------------")
    17.         sw.WriteLine(String.Concat(TypeName(e), " occurred ", DateTime.Now))
    18.         sw.WriteLine(String.Concat("Error  : ", e.Message))
    19.         sw.WriteLine(String.Concat("Source : ", e.Source))
    20.         sw.WriteLine(String.Concat("Target : ", e.TargetSite))
    21.         sw.WriteLine(String.Concat("Stack  : ", e.StackTrace))
    22.         sw.Flush()
    23.         sw.Close()
    24.         fs.Close()
    25.         sw = Nothing
    26.         fs = Nothing
    27.     End Sub
    28.  
    29. 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.

  5. #5
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    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:
    1. Dim myTraceLog as New System.IO.FileStream("C:\myTraceLog.txt", IO.FileMode.OpenOrCreate)
    2. ' Creates the new trace listener
    3. Dim myListener As New TextWriterTraceListener(myTraceLog)

    Now add the listener to the trace.listeners collection:-

    VB Code:
    1. 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:
    1. 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:
    1. Debug.Write("Error in procedure line5 etc. yada yada")
    2. 'or Debug.WriteLine("Another bug - for crying out loud!")
    3. 'or try some nice indentation for easy reading
    4. Debug.WriteLine("Staring my custom procedure")
    5. Debug.Indent
    6. Debug.WriteLine("Processed first section")
    7. Debug.WriteLine("Finished custom procedure")
    8. Debug.Unindent
    9. 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.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Hey thats cool Musician, thanks!

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    To get it to work with the Debug statements I had to set AutoFlush to true (just in case anyone else tries).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width