?
Solved

C#: Log file append size limit, create new and name by datetime ???

Posted on 2004-10-01
5
Medium Priority
?
7,422 Views
Last Modified: 2008-02-26
The following excellent code was provided to me my EE 'eternal 21'..... It records data bytes line-by-line very nicely.......

Q. How can I limit the log file size and create a new file with a slightly different name (timedate, etc...)???


byte[] newLine = System.Text.ASCIIEncoding.ASCII.GetBytes(Environment.NewLine);

  fs = new FileStream("SyslogTest.txt", FileMode.Append, FileAccess.Write, FileShare.Read);
  using(fs)
  {
    Byte[] sendBytes = Encoding.ASCII.GetBytes(machineName + str);
    fs.Write(sendBytes, 0, sendBytes.Length);
    fs.Write(newLine, 0, newLine.Length);
  }
0
Comment
Question by:kvnsdr
[X]
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 Comments
 
LVL 8

Expert Comment

by:Razzie_
ID: 12201022
Have a look at http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21120146.html

Once you detected that a file is too long, you can easily create new logfile using the old filename and add '_2' to the name, or use a date.

HTH,

Razzie
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12201416
  The following code (above link) deletes the original file by name and creates a file. However, the file will have a different name each time, so "log.txt" will be a variable not a constant.

string fileName = "log.txt";
FileStream fs = new FileStream(fileName,FileMode.Open);
int length = fs.Length;
if(length > 500)
{
     File.Delete(fileName);
}
if(File.Exists = false)  // After deleting the file u can recreate a file with the same name
{
     filestream = File.Create(fileName);
}
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 12201636
What I do most of the time is to add yyyyMM to the filename so each month, a new file is created. In my case, I don't have size problem within a month.
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12203033
I'm getting a lot of advice, but will anyone help me with the actual code??? I need to create a new log file after the current one reaches either a specific size or by date. The file grows so fast that creating a new file each day will probably work fine...... Below is the actual log writing code......

Here's my first posting........

The following excellent code was provided to me my EE 'eternal 21'..... It records data bytes line-by-line very nicely.......

Q. How can I limit the log file size and create a new file with a slightly different name (timedate, etc...)???


byte[] newLine = System.Text.ASCIIEncoding.ASCII.GetBytes(Environment.NewLine);

  fs = new FileStream("SyslogTest.txt", FileMode.Append, FileAccess.Write, FileShare.Read);
  using(fs)
  {
    Byte[] sendBytes = Encoding.ASCII.GetBytes(machineName + str);
    fs.Write(sendBytes, 0, sendBytes.Length);
    fs.Write(newLine, 0, newLine.Length);
  }
0
 
LVL 3

Accepted Solution

by:
a_goat earned 2000 total points
ID: 12212335
System.IO.StreamWriter LogWriter = new System.IO.StreamWriter(new FileStream("SyslogTest_0.txt", FileMode.Append, FileAccess.Write, FileShare.Read));

int DataSize = 0;
int FileCount = 1;
const int MAX_FILE_SIZE = 1000000 // One Meg

public void Log (string Data)
{
DataSize += Data.Length;
if (DataSize > MAX_FILE_SIZE)
{
DataSize = Data.Length;
LogWriter.Close();
LogWriter = new System.IO.StreamWriter(new FileStream(string.Format("SyslogTest_{0}.txt", FileCount++), FileMode.Append, FileAccess.Write, FileShare.Read));
}
LogWriter.WriteLine(Data);
}


BTW, eternal21's answer can be shortened to


System.IO.StreamWriter lw = new System.IO.StreamWriter(new FileStream("SyslogTest.txt", FileMode.Append, FileAccess.Write, FileShare.Read));

lw.WriteLine(machineName + str);


Far more efficient and cleaner

Also,
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21154056.html The suggestions in there deal with this as well.
0

Featured Post

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!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

801 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