everyday a new file?

  • Thread starter Thread starter Dirk Reske
  • Start date Start date
Dirk,

How do you log the events? I'm assuming that you have one API call (or
a limited handful) which manages how the log file is opened, and then
written to.

All you have to do when you make the call is check the filename of the
file stream you have open (if you keep it open all the time). If it doesn't
match with the time the call is made, then close it and open a new one.

You can get the filename by calling the ToString method on the current
DateTime, passing the following format:

yyyyMMdd

This will also have the effect of making the files sortable by date.

Hope this helps.
 
Dirk,
if (this.FileCreateDate.Date < DateTime.Today.Date)
//create new file
else
//write to the open File

It shouldn't be that complicated. Try something like:


FileStream fs = new FileStream(DateTime.Now.ToString("yyyyMMdd")+".log",
FileMode.OpenOrCreate);


By using FileMode.OpenOrCreate the file will open the file or create the
file it it does not exist.


Good luck
Brian W
 
How about just using log4Net. They have already built this functionality
and much more.
 
Hello,

I write an app that, logs events.
how can I make, that everyday a new file whould be createt, with the date as
name?

thx
 
when I create the File I set the global DateTime variable FileCreateDate to
the aktuall date.

before I write to the file I check the date:

if (this.FileCreateDate.Date < DateTime.Today.Date)
//create new file
else
//write to the open File

but this doesn't work, if I have a new day

Nicholas Paldino said:
Dirk,

How do you log the events? I'm assuming that you have one API call (or
a limited handful) which manages how the log file is opened, and then
written to.

All you have to do when you make the call is check the filename of the
file stream you have open (if you keep it open all the time). If it doesn't
match with the time the call is made, then close it and open a new one.

You can get the filename by calling the ToString method on the current
DateTime, passing the following format:

yyyyMMdd

This will also have the effect of making the files sortable by date.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dirk Reske said:
Hello,

I write an app that, logs events.
how can I make, that everyday a new file whould be createt, with the
date
as
name?

thx
 
Back
Top