error.log archiving

Posted on 2006-04-20
Last Modified: 2010-04-17
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.


Will some one tell me how I cane do it?
Question by:cdb424ttm
    LVL 11

    Expert Comment

    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();
                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;
                    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);

    Author Comment

    where do I put this code you gave me?
    LVL 11

    Expert Comment

    Sorry, into a C# application what programming language are you the most familiar with?

    Author Comment

    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.
    LVL 11

    Expert Comment

    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.
    LVL 11

    Accepted Solution

    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
        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
            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

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    Add mobile access to browser application 3 106
    sameEnds challenge 25 55
    find a node in VST 2 36
    countX 22 48
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now