Solved

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

Posted on 2004-10-01
5
7,197 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_
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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

713 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