[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-10-01
5
Medium Priority
?
7,666 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

873 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