Need advice with log file

Posted on 2009-06-29
Last Modified: 2012-05-07
I'm using VB 2005, WinForms. I have a procedure that logs events that I need logged in my application. The procedure works nicely for my needs. It produces a text log file that is nothing more than line entries on a page - a typical event logger, one entry after the other.

Currently, the procedure does not terminate the log file, so the file could theoretically grow and grow in size without limits. -- Here's where I need advice:

1). Is there a way to have the file grow to a certain size (perhaps 500 entries long) and then begin to delete the oldest entry, so that it can add a new one - like FIFO?
2). Would it be better (or easier)  to close the log file out at 500 entries and start a new file like the old Unix logs that logged up to 10 files of history (i.e. log01, log02, log03...etc)?
Has anyone worked on anything like this, who can provide advice and perhaps some sample code?

Question by:Mr_Fulano
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
LVL 53

Expert Comment

ID: 24742736
It all depends on your program.
1. Does the program runs all the time ? I that is the case, I would keep track of an index of how many lines you already wrote to a file. When this reach a certain limit, start a new file. (I've done this in a critical application that needed to log almost everything, logfiles of about 10 MB - 50 files a day)

Expert Comment

ID: 24742777
Hi Mr_Fulano,
If it is a database application, you can keep your logs (in your database) in a safe place that could me maintained from everywhere and every time. You can delete delete periodically, delete by size from your application, from database management system or from another web or windows application.
LVL 48

Expert Comment

ID: 24742902
>> If it is a database application, you can keep your logs (in your database)
And if you cannot connect to the database, where do you write that log ?
Can you tell us how many records will you have per month (more or less) or what you're recording ? You can use log files with more then 50mb without any problem, but do you need to read that information later ?

I normally use the *.log file to write only errors and use the db to log the actions (delete, update, login, etc).
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 24744210
Yes you are right jpaulino but Mr_Fulano wants an event log.
It is true for example, if you get an error while connecting to the database and not succeeded and want to log this situation, you don't have any option to log the error in a different place other then the DB. So if Mr_Fulano also want to keep an error log, he must do the what you said.
Also having a good exception handling management is very important in this stuation.


Author Comment

ID: 24750643
Hi Everyone, first and foremost thank you for your comments and help. I'll try and provide as much info as possible to help you all understand what I need...

1). The logs are intended for audit purposes. To track who changed what.
2). The log contains "changes" made to the data within a database.
3). Events are logged when they occur.
4). The log is written out to a text file, so that it can be exported to Excel or a comma delimited file.
5). The logs are written to a folder on the server. Only System Admins have access to the log files themselves.
6). There is no way to determine how many entries the log will have because if no changes are made to the data, then logging does not occur. If changes are continuously made to the data, then I'll have a lot of log entries.
7). I think that cutting the logs off at 500 lines would be OK. (One line per entry).
8). Right now the logs do not have a cut off point.

My options are to make the logs revolving (i.e. First In First Out). Or, after 500 lines, close that file out and begin a new one (i.e Log01, Log02, etc). I tend to think that since the root need for the logs is for auditing purposes, then I shouldn't removed any lines from the logs to replace them with new lines like in a revolving log. I think that a new file would be best, so that all changes can be accounted for during an audit.

What would you recommend and what would be most practical? -- Also, how would you approach your suggested choice?

Thanks for your help,
LVL 48

Expert Comment

ID: 24751923
Hi Fulano,

To create a FIFO you need to read the file to a StreamReader, and then write to a StreamWriter only the 500 entries, and then write to disk. I don't like this process.

If you write 500 entries and then create a new file, you will have allot of work to search for a log entry. You could have entries in two different files for one day.

I think that the most easy and practical process is to write a file per day, week or month, depending of the entries you will have. It's easy to implement and easy to archive.

Author Comment

ID: 24752025
Hi JPaulino, nice to hear from you. Hope all has been well.

I like your idea of a period based scheme, because it gives the logs a sense of time reference, but when you say "I think that the most easy and practical process is to write a file per day, week or month, depending of the entries you will have. It's easy to implement and easy to archive."

How would you implement and archive such a scheme? -- Do you have a simple example for me to review?

Thanks for your help,
LVL 48

Accepted Solution

jpaulino earned 500 total points
ID: 24752158
I'm fine Fulano, thanks for asking. I hope you're file also!
It's very easy to implement, because it creates a new file if doesn't exists or appends if exists. I have used My namespace.
Here's an easy example that you can use in the all project. It will write a log file for each day but if you can change it for week base, if you like. You can use Weekday(Now) to get the weekname.

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        log.writeLog(Now.ToString & " This is a text ... ")
    End Sub
End Class
Public Class log
    ''' <summary>
    ''' Writes to the log file
    ''' </summary>
    ''' <param name="str">Message to write</param>
    ''' <remarks></remarks>
    Public Shared Sub writeLog(ByVal str As String)
        Dim logDir As String = "d:\"
        Dim logPath As String = String.Format("{0}{1}.log", logDir, Now.ToString("yyyyMMdd"))
        My.Computer.FileSystem.WriteAllText(logPath, str & vbNewLine, True)
    End Sub
End Class

Open in new window

LVL 48

Expert Comment

ID: 24752167
typo: "I hope you're file also!"
 fine not file :)

Author Comment

ID: 24759447
Hi JPaulino, thank you for the great example. Its times like this when I wish I could award more than the maximum points allowed, because you did a fantastic job. Your suggestion is simple, precise and effective and is what I was looking for in a solution.

My only question is about the line below:

>> My.Computer.FileSystem.WriteAllText(logPath, str & vbNewLine, True)

Will that work if "logPath" is on a networked drive? -- I ask because of the
"My.Computer" parameters.

Thanks again!

Author Closing Comment

ID: 31598234
LVL 48

Expert Comment

ID: 24761086
Thanks Fulano
I think you have tried already, but yes, it works for network drives.
Glad I could help!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question