הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Friday, March 26, 2010 1:50 PM
I am using the StreamWriter to create a log file for my application. However I do not see a method on how to clear the file (make the file blank again). Also before I clear the file I want to rename it so that I can have multiple log files, a new one produced everytime the application runs.
For example the "normal" name will be something like AULog. When the log closes I want it to be renamed to AULog1 the first time the program runs, then AULog2 the second time and so on. After lets say 8 Log files have been created I would like to start overwritting the previous log files starting with AULog1. I have included the little code I have making the log.
Any help or suggestions would be greatly appreciated.
Public Sub LogWrite(ByRef ex)
' Create an instance of StreamWriter to write text to a file.
Using sw As StreamWriter = File.AppendText("C:\AU_Test\Excel\AULog.txt")
sw.WriteLine("Log Entry: ")
sw.Write("The date is: ")
sw.WriteLine(DateTime.Now)
sw.Write("An exception occured: ")
sw.WriteLine(ex)
sw.WriteLine(" ")
sw.Close()
End Using
End Sub
All replies (9)
Monday, March 29, 2010 7:03 PM ✅Answered
If you use code similar to what I suggested, then there will be a file per day. If you use GetFiles() on the directory and sort the results you should be able to delete those over your limit.
Something like this though you might need to reverse the sort. this is just from the hip :-)
Public Sub LogWriter(ByVal ex As Exception, ByVal st As State)
Const logDir As String = "C:\Documents and Settings\u13348\Desktop\Mike Dupuis\VB Project\AU1\"
Const logFileTemplate As String = "AULog_{0}.txt"
Const logEntryTemplate As String = "Timestamp: ""{0}""; Event: ""{1}""; State: ""{2}"""
Dim logFile As String = String.Format(String.Concat(logDir, logFileTemplate), Date.Today().ToString("yyyyMMdd"))
Dim logEntry As String = String.Format(logEntryTemplate, Date.Now(), ex.Message, st)
File.AppendAllText(logFile, logEntry)
Dim existingLogs() As String = Directory.GetFiles(logDir)
If 5 < existingLogs.Length Then
Array.Sort(existingLogs)
For Each s As String In existingLogs.Skip(5)
File.Delete(s)
Next
End If
End Sub
jon.stromer.galley
Friday, March 26, 2010 2:23 PM
File.Move() can be used to rename a file
Rather than creating the StreamWriter via File.AppendText() you can create the writer yourself. At that point to can control if the file is appended to or if it is created from scratch.
jon.stromer.galley
Friday, March 26, 2010 4:16 PM
Thanks for the reply. I tried changing some lines of my code. I will attach what I have now below. I get an error on the "With file.AppendText(Path & ".txt")" line. the error is an IOException and is says the file is being used by another process. Any idea why?
Public Sub LogWriter()
Dim count As Integer
count = 0
Dim Path As String
Path = "C:\Documents and Settings\u13348\Desktop\Mike Dupuis\VB Project\AU1\AULog"
If File.Exists("C:\Documents and Settings\u13348\Desktop\Mike Dupuis\VB Project\AU1\AULog.txt") Then
count += 1
File.Create(Path & count & ".txt")
With File.AppendText(Path & count & ".txt")
.WriteLine("Log Entry: ")
.Write("The date is: ")
.WriteLine(DateTime.Now)
.Write("An exception occured: ")
.WriteLine(ex)
.WriteLine(state)
.WriteLine(" ")
.Close()
.Dispose()
End With
Exit Sub
End If
File.Create(Path & ".txt")
' Create an instance of StreamWriter to write text to a file.
'Using sw As StreamWriter = File.AppendText("C:\AU_Test\Excel\AULog.txt")
'Using sw As StreamWriter = File.AppendText("C:\Documents and Settings\u13348\Desktop\Mike Dupuis\VB Project\AU1\AULog" & count & ".txt")
With File.AppendText(Path & ".txt")
.WriteLine("Log Entry: ")
.Write("The date is: ")
.WriteLine(DateTime.Now)
.Write("An exception occured: ")
.WriteLine(ex)
.WriteLine(state)
.WriteLine(" ")
.Close()
.Dispose()
'End Using
End With
End Sub
Friday, March 26, 2010 5:02 PM
How about something like this?
Imports System.IO
Module Module1
Class State
End Class
Sub Main()
LogWriter(New Exception("bla"), New State())
End Sub
Public Sub LogWriter(ByVal ex As Exception, ByVal st As State)
Const logDir As String = "C:\Documents and Settings\u13348\Desktop\Mike Dupuis\VB Project\AU1\"
Const logFileTemplate As String = "AULog_{0}.txt"
Const logEntryTemplate As String = "Timestamp: ""{0}""; Event: ""{1}""; State: ""{2}"""
Dim logFile As String = String.Format(String.Concat(logDir, logFileTemplate), Date.Today().ToString("yyyyMMdd"))
Dim logEntry As String = String.Format(logEntryTemplate, Date.Now(), ex.Message, st)
File.AppendAllText(logFile, logEntry)
End Sub
End Module
jon.stromer.galley
Monday, March 29, 2010 5:35 PM
Ok I have the logging set to how I want it. However, I cannot figure out how to get the files to be overwritten. For example say I want a maximum of 5 log files (one for every day of the business week). Upon starting a new week I would like to overwrite the previous files starting with the oldest file. Does anyone have any suggestions on how to do this? I tried using the File.GetCreationTime command but I am not really sure how that works.
Thanks in advance
Tuesday, March 30, 2010 11:33 AM
Ok I did not understand what you were getting at previously. Thanks for the help.
Wednesday, March 31, 2010 1:47 PM
On the line:
For Each s As String In existingLogs.Skip(5)
I am getting an error Skip is not a member of System.Array. Is there somehting I can switch skip to? I will continue to investigate this problem.
Wednesday, March 31, 2010 1:47 PM
On the line:
For Each s As String In existingLogs.Skip(5)
I am getting an error Skip is not a member of System.Array. Is there somehting I can switch skip to? I will continue to investigate this problem.
Wednesday, March 31, 2010 3:45 PM
If you are not getting the extension method you could do it via:
For i As Int32 = 5 To existingLogs.Count - 1
File.Delete(existingLogs(i))
Next
jon.stromer.galley