Author Topic: BasicDataLogger file size limiting  (Read 3088 times)

ijones

  • Newbie
  • *
  • Posts: 8
    • View Profile
BasicDataLogger file size limiting
« on: April 03, 2015, 06:03:55 PM »
Can anyone tell me if there is a simple way to limit the size of log files created by the BasicDataLogger control?
More specifically, is there a way to set a time frame such as I only want a days worth of data in the log then have the old data roll off?


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: BasicDataLogger file size limiting
« Reply #1 on: April 03, 2015, 06:17:01 PM »
Here is a quick easy way to stop the logging after so many points:

- Open the code for BasicDataLogger.vb
- In the properties region add this code:

    Private m_MaximumPoints As Integer
    Public Property MaximumPoints As Integer
        Get
            Return m_MaximumPoints
        End Get
        Set(value As Integer)
            m_MaximumPoints = value
        End Set
    End Property


- Go to the Event Region and modify the DataChanged event handler like this:

    Private PointCount As Integer
    Protected Overrides Sub onDataChanged(ByVal e As MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs)
        MyBase.OnDataChanged(e)

        If m_LogTriggerType = TriggerType.DataChange Then
            If m_MaximumPoints > 0 AndAlso PointCount < m_MaximumPoints Then
                StoreValue()
                PointCount += 1
            End If
        End If
    End Sub



Rolling off old data will require a little more code because you will have to delete the first line of the file.

ijones

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: BasicDataLogger file size limiting
« Reply #2 on: April 04, 2015, 12:22:54 PM »
Thanks for the reply!
You're right on to what I'm trying to accomplish. I do need to 'roll off' the old lines when the file reaches that 'MaximumPoints' size.

Seems like it ought to be easy but from what I've thought and read, one must check the files size, and if too long, you must read in the file, then write it back minus the first line..... and if the file contains very many points, this can be quite burdensome on the machine.
Would be sweet if there was a way to delete the first line of text in a file as it is to append a new line to the end...

I feel like I may be getting away from the BasicDataLogger control and that's precisely what I don't want to do!!

Any suggestions??

I have been working with some of the stuff Godra posted on his Quick Chart and liking it!

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: BasicDataLogger file size limiting
« Reply #3 on: April 04, 2015, 03:10:54 PM »
The way I'm thinking, the easiest thing for you to do is to follow these steps:

1) Create a daily copy of the log file at a certain time of the day (let's say Midnight)
2) Stop logging and delete original log file
3) Re-create original file and continue logging

All together you would have three log files at any time (PLCDataLog.log, CopyofPLCDataLog.log and new DailyCopyofPLCDataLog.log).

You can run one instance of Quick Daily Chart which would be using CopyofPLCDataLog.log, which whenever used is only a copy of the current PLCDataLog.log.

You can also modify the solution and run another instance of Quick Daily Chart which would be using DailyCopyofPLCDataLog.log (this instance you would run only when you might need it).

Quick Daily Chart code already has this line:

Code: [Select]
File.Copy("C:\PLCDataLog.log", "C:\CopyofPLCDataLog.log", True)

------------------

The following lines should be used with some sort of a timer, checking for that specific time of the day when you want to perform those actions, inside BasicDataLogger.vb:

Creating a new Daily copy of the log file would use this line:

Code: [Select]
File.Copy("C:\PLCDataLog.log", "C:\DailyCopyofPLCDataLog.log", True)

Deleting the original log file would use this line:

Code: [Select]
File.Delete("C:\PLCDataLog.log")

Re-creating that same log file would use this line:

Code: [Select]
File.Create("C:\PLCDataLog.log")

I am not sure if the StreamWriter needs to be closed to release the log file so it could be deleted and re-created.

Archie might suggest a possible code.

It might be easier just to do all these things once the maximum number of points has been reached.
« Last Edit: April 04, 2015, 03:32:19 PM by Godra »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: BasicDataLogger file size limiting
« Reply #4 on: April 04, 2015, 04:35:27 PM »
I also think the only way to do this is to read the entire file into memory and write it back without the first line. Here is a post on another forum showing one technique:

http://stackoverflow.com/questions/2099505/how-to-delete-the-first-and-last-lines-from-a-text-file-in-visual-basic

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: BasicDataLogger file size limiting
« Reply #5 on: April 06, 2015, 08:46:31 PM »
Archie,

Your programming knowledge goes far beyond mine and I will ask you if you can take a look at this post:

http://stackoverflow.com/questions/12804488/setting-position-index-of-filestream-seek-to-retrieve-blocks-of-data-vb-net

At the end of the post there is a short code which allows to truncate log files to keep them in a specific size (I am not sure if something similar might be included as a feature of the BasicDataLogger) and to me this sounds like what ijones has asked for.

ijones

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: BasicDataLogger file size limiting
« Reply #6 on: April 07, 2015, 09:11:26 PM »
Thanks for the suggestions and advice guys!
I had seen the post that Archie referred to and had started playing with it but ran out of time and haven't gotten back to it. And in the meantime, I had also thought of doing just what you had suggested Godra. So! I think we're all thinking similar thoughts. Just need the time to put some to the test!
I will update when I can.. Life has gotten busy all of a sudden and it may be some time before I get anywhere with this. But thanks again for the help!