Can any one give me code for finding size of my log file using VB.NET????
Also I want the code to make my log file of 1KB only and not more than that.
:'(

Dani AI

Generated

A concise, practical plan for keeping a text log at 1 KB and removing old entries.

’s FileInfo approach is fine for detecting size; once the file exceeds the limit there are two reliable strategies: rotate the file (rename the current log and start a new one) or prune the existing file (remove oldest entries or entries older than a given date until size ≤ 1 KB). Rotation is the simplest and safest for production; pruning is useful when a single-file history must be preserved. asked for date-based deletion — that is best implemented as a line-based filter (parse each log line’s timestamp), then trim oldest lines if size still exceeds the threshold.

Recommended workflow (robust, safe):

  1. Detect size (use the simple FileInfo check already suggested by @MehdiAnis).
  2. If size > 1024 bytes and date-based pruning is required, read the file as lines, parse timestamps using the actual format your logger emits, keep only lines newer than the cutoff.
  3. If the file is still too large, remove oldest lines until the joined content’s byte length (use the intended file encoding) is ≤ 1024. Always write results to a temp file in the same folder, then overwrite the original (copy/replace) to avoid truncating while a writer is active.
  4. Handle IO exceptions (file-in-use, permissions) and test on a copy first.

Example VB.NET (line-based prune + size trim — adapt timestamp parsing to your log format):

' Keep newest lines and optionally remove lines older than dateCutoff.
Function PruneLog(filePath As String, maxBytes As Integer, Optional dateCutoff As System.Nullable(Of DateTime) = Nothing) As Boolean
    If Not System.IO.File.Exists(filePath) Then Return False
    Dim allLines = System.IO.File.ReadAllLines(filePath)
    Dim kept As New System.Collections.Generic.List(Of String)
    For Each line In allLines
        If dateCutoff.HasValue Then
            Dim parsed As DateTime
            Dim keepLine = True
            If line.Length >= 10 AndAlso DateTime.TryParse(line.Substring(0, Math.Min(19, line.Length)), parsed) Then
                If parsed < dateCutoff.Value Then keepLine = False
            End If
            If keepLine Then kept.Add(line)
        Else
            kept.Add(line)
        End If
    Next
    While System.Text.Encoding.UTF8.GetByteCount(String.Join(Environment.NewLine, kept)) > maxBytes AndAlso kept.Count > 0
        kept.RemoveAt(0)
    End While
    Dim tempPath = filePath & ".tmp"
    System.IO.File.WriteAllLines(tempPath, kept, System.Text.Encoding.UTF8)
    System.IO.File.Copy(tempPath, filePath, True)
    System.IO.File.Delete(tempPath)
    Return True
End Function

Notes and caution: adjust the timestamp parsing to match the log format, choose the correct encoding when measuring bytes, and prefer rotation for concurrent/multi-process logging. As warned, always handle read-only and in-use exceptions; test on backups before applying to production logs.

Recommended Answers

All 6 Replies

Can any one give me code for finding size of my log file using VB.NET????
Also I want the code to make my log file of 1KB only and not more than that.
:'(

I want vb.net code for getting file size for a particular log file and delete data based on date if the file size exceeds the limit.

please send me the code for the above mentioned one..

To Get File Size:
=============

Imports System.IO

Private Function GetFileSize(ByVal MyFilePath As String) As Long
Dim MyFile As New FileInfo(MyFilePath)
Dim FileSize As Long = MyFile.Length
Return FileSize
End Function


DELETE COMMAND:
============
MyFile.Delete()

Make sure the file is not ReadOnly, AlreadyInUse B4 deleting, or u get exception.

Thanks for the code

I need the code for opening the file and deleting based on date..

please help me.. the other code was really helpfull..

3 years later but thanks for the code!!!
always nice to quickly get a working sample code from a simple google search.

yoann (www.prodeos.fr)

I'm glad you got it helpful.

Please do not resurrect old threads. If you have any questions please ask. You are welcome to start your own threads.

Have a look at forum rules.
Please read before posting - http://www.daniweb.com/forums/thread78223.html

Thread Closed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.