Solved

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

Posted on 2004-10-01
5
6,882 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
5 Comments
 
LVL 8

Expert Comment

by:Razzie_
Comment Utility
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
Comment Utility
  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 69

Expert Comment

by:Éric Moreau
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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

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

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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