Solved

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

Posted on 2004-10-01
5
7,129 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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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