Solved

Need advice with log file

Posted on 2009-06-29
12
335 Views
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?
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
0
Comment
Question by:Mr_Fulano
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 53

Expert Comment

by:Dhaest
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)
0
 
LVL 5

Expert Comment

by:kosturdur
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.
0
 
LVL 48

Expert Comment

by:jpaulino
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).
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Expert Comment

by:kosturdur
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.

 
0
 

Author Comment

by:Mr_Fulano
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,
Fulano
0
 
LVL 48

Expert Comment

by:jpaulino
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.
0
 

Author Comment

by:Mr_Fulano
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,
Fulano
0
 
LVL 48

Accepted Solution

by:
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

0
 
LVL 48

Expert Comment

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

Author Comment

by:Mr_Fulano
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!
Fulano
0
 

Author Closing Comment

by:Mr_Fulano
ID: 31598234
Excellent!!!
0
 
LVL 48

Expert Comment

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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

816 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now