Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

error.log archiving

Hi all,
What I’m looking to do is rename the error.log if it has a line in it which has Out of Date in it anywhere. I’m using xp pro. We use a 3rd party to write the error into the log file.
So what I want to happen is when a out of date line is wrote to the error.log that the
File will rename the error.log to some thing like this.

Error4_20_06_1.log
Error4_20_06_2.log
Error4_20_06_3.log

Will some one tell me how I cane do it?
0
cdb424ttm
Asked:
cdb424ttm
  • 4
  • 3
1 Solution
 
LordWabbitCommented:
Here is some code which should do the trick, however there are some things to consider...
- If your logs get really big then this is not the right solution
- If this app is busy reading your log as the third party tries to write to it the file will be in use and they wont be able to write.

        private void button1_Click(object sender, EventArgs e)
        {
            string PathToLogFile = @"C:\eatme\log.log";
            StreamReader myReader = new StreamReader(PathToLogFile);
            string strLog = myReader.ReadToEnd();
            myReader.Close();
            if (strLog.Contains("Out of Date"))
            {
                int intHighestFileNum = 1;
                foreach (string eleFile in System.IO.Directory.GetFiles("C:\\eatme\\Logging"))
                {
                    string[] arrBits = eleFile.Split((char)95);
                    int intFileNum = Convert.ToInt32(arrBits[3].Substring(0, arrBits[3].IndexOf(".")));
                    if (intFileNum > intHighestFileNum) intHighestFileNum = intFileNum;
                }
                intHighestFileNum++;
                string strDestPath = "C:\\eatme\\Logging\\Error" + DateTime.Now.Month + "_" + DateTime.Now.Day + "_" + DateTime.Now.Year.ToString().Substring(2) + "_" + intHighestFileNum.ToString() + ".log";
                System.IO.File.Copy(PathToLogFile, strDestPath);
            }            
        }
0
 
cdb424ttmAuthor Commented:
where do I put this code you gave me?
0
 
LordWabbitCommented:
Sorry, into a C# application what programming language are you the most familiar with?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
cdb424ttmAuthor Commented:
Sorry vb6 is only thing I know I do have dot net but have not used it yet. I will need you to walk me through it.
0
 
LordWabbitCommented:
Not a problem will recode it for you, was wondering if you wanted the orginal log deleted after backing it up?  If not then the next time the log is checked it will pick up Out of Date again and archive the log again.
0
 
LordWabbitCommented:
Eeeeuuaarggh and to think I used to think vb6 was the greatest thing since sliced bread.  Dotnet has spoiled me rotten.
You will need to add the Microsoft Scription Runtime reference to your project.

Private Sub timerMain_Timer()
    Dim strPathToLogFile As String
    Dim strDestPath As String
    Dim strLine As String
    Dim intHighestFileNum As Integer
    Dim intFileNum As Integer
    Dim arrBits() As String
    Dim strFileName As String
    Dim oFileSystem As Scripting.FileSystemObject
    Dim oTextFile As Scripting.TextStream
    Dim oFolder As Scripting.Folder
    Dim oFiles As Scripting.Files
    Dim oFile As Scripting.File
    '-=-
    strPathToLogFile = "C:\eatme\log.log"
    strDestPath = "C:\eatme\Logging"
    Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    If oFileSystem.FileExists(strPathToLogFile) = False Then Exit Sub
    Set oTextFile = oFileSystem.OpenTextFile(strPathToLogFile, ForReading)
    strLine = oTextFile.ReadAll
    oTextFile.Close
    If InStr(strLine, "Out of Date") > 0 Then
        '-=- Find the highest file number
        Set oFolder = oFileSystem.GetFolder(strDestPath)
        For Each oFile In oFolder.Files
            arrBits = Split(oFile.Name, "_")
            intFileNum = CInt(Mid(arrBits(3), 1, InStr(arrBits(3), ".")))
            If intFileNum > intHighestFileNum Then intHighestFileNum = intFileNum
        Next
        intHighestFileNum = intHighestFileNum + 1
        strFileName = "Error" & Format(Now, "mm_dd_yy") & "_" & intHighestFileNum & ".log"
        oFileSystem.CopyFile strPathToLogFile, strDestPath & "\" & strFileName, False
        oFileSystem.DeleteFile strPathToLogFile
        Set oFolder = Nothing
    End If
    Set oTextFile = Nothing
    Set oFileSystem = Nothing
End Sub
0
 
cdb424ttmAuthor Commented:
Thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now