Need advice with log file

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?
or
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?

Thanks,
Fulano
Mr_FulanoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
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)
0
kosturdurCommented:
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.
0
Jorge PaulinoIT Pro/DeveloperCommented:
>> 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).
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

kosturdurCommented:
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.

 
0
Mr_FulanoAuthor Commented:
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,
Fulano
0
Jorge PaulinoIT Pro/DeveloperCommented:
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.
0
Mr_FulanoAuthor Commented:
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,
Fulano
0
Jorge PaulinoIT Pro/DeveloperCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jorge PaulinoIT Pro/DeveloperCommented:
typo: "I hope you're file also!"
 fine not file :)
0
Mr_FulanoAuthor Commented:
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!
Fulano
0
Mr_FulanoAuthor Commented:
Excellent!!!
0
Jorge PaulinoIT Pro/DeveloperCommented:
Thanks Fulano
I think you have tried already, but yes, it works for network drives.
Glad I could help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.