Solved

Need advice with log file

Posted on 2009-06-29
12
329 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to get a file from SqlServer DB 4 29
MS access and calling saved Queries 4 26
Hide Tab Page 3 20
Round up to 100% in .NET 10 45
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

15 Experts available now in Live!

Get 1:1 Help Now