CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: log file

  1. #1
    Join Date
    Dec 2004
    Location
    Pakistan(Skardu)
    Posts
    64

    Question log file

    hi experts,
    i am going to create a log file in vb.net...
    when i write a text file its overwrite previous text..so how can i append text
    or any sugestion about create log file

  2. #2
    Join Date
    Dec 2004
    Location
    Pakistan(Skardu)
    Posts
    64

    Exclamation create Log file

    hi experts,
    i am going to create a log file in vb.net...
    when i write a text file its overwrite previous text..so how can i append text
    or any sugestion about create log file

  3. #3
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: create Log file

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: log file

    As a VB6 user in the VB6 forum I'd answer
    Code:
    Open Filename For Append as #1
    But since you asked for VB.NET, the answer is different.
    If you opened your file as a StreamWriter you'd have to use
    Code:
    myStreamWriter = File.AppendText(txtFileName.Text)
    More detailed answers you'd certainly get faster in the VB.NET forum.

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: log file

    Quote Originally Posted by kku View Post
    hi experts,
    i am going to create a log file in vb.net...
    when i write a text file its overwrite previous text..so how can i append text
    or any sugestion about create log file
    Use the streamwriter method and set append = true

    Code:
     
    Dim file As New System.IO.StreamWriter("\LogFile.txt", True)
    file.WriteLine(logtext)
    file.Close()
    The variable logtext of course should have the text you want to log prior to the above code.

  6. #6
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: log file

    Quote Originally Posted by kku View Post
    hi experts,
    i am going to create a log file in vb.net...
    when i write a text file its overwrite previous text..so how can i append text
    or any sugestion about create log file
    Each time you generate a new log line, store it on an arraylist, like YourArrayList
    Each time you want it to be saved, use
    Code:
            Dim fs As FileStream = File.Open(LogFilePath, FileMode.OpenOrCreate)
            Dim sr As New StreamWriter(fs)
            sr.Write(String.Join(vbCrLf, YourArrayList))
            sr.Close()
    [Vb.NET 2008 (ex Express)]

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: log file

    Not likely.
    Code:
    .OpenOrCreate
    If you write the array again and again, it will append a NEW list to the end of the old list. Eventually, something will grow too big...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: log file

    Quote Originally Posted by dglienna View Post
    Not likely.
    Code:
    .OpenOrCreate
    If you write the array again and again, it will append a NEW list to the end of the old list. Eventually, something will grow too big...
    You are right. Maybe it should .clear the arraylist after saving.
    [Vb.NET 2008 (ex Express)]

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: log file

    WHY reinvent the wheel. The Enterprise Library Logging block will meet you needs perfectly. No code to write, simply link it in, update your config and your are done (except for adding the logging calls at appropriate locations in your program).

    btw: The same is true for DataAcces, ExceptionHandling, Advanced Configuration, Dependancy Injection, and a whole slew of other common tasks.

    I have not written a (non-trivial non-throwaway) application without using EL in over 4 years. I have even updated all my VS project templates to automatically setup and configuration my projects upon creation.
    Last edited by TheCPUWizard; December 10th, 2008 at 09:36 AM. Reason: Fixed spelling error.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

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

Featured