true image

true image

  • NA
  • 3
  • 2.8k

C# Code to parse a text file and calculate time difference

May 28 2013 9:22 PM
Hi, I'd like to parse a log file and grab the timestamps at the start of specific lines in pairs and then find the time difference to see how long it took.

Here's a dummy example of the file contents:

2013-05-28 16:39:54,185 [http-8080-8 UserName] DEBUG DynamicDialogService.java:395 - DynamicDialogService.getDialogDescriptors()

2013-05-28 16:39:54,935 [http-8080-8 UserName] DEBUG ControlDescriptor.java:213 - Populating value of control Control1
2013-05-28 16:39:54,935 [http-8080-8 UserName] DEBUG ControlDescriptor.java:266 - Completed population of control 'Control1', value: 'test'

2013-05-28 16:39:55,060 [http-8080-8 UserName] DEBUG ControlDescriptor.java:213 - Populating value of control Control2
2013-05-28 16:39:55,060 [http-8080-8 UserName] DEBUG ControlDescriptor.java:266 - Completed population of control 'Control2', value: '2'

2013-05-28 16:39:55,076 [http-8080-8 UserName DEBUG ControlDescriptor.java:213 - Populating value of control Control3
//many lines of logging
2013-05-28 16:39:55,154 [http-8080-8 UserName] DEBUG ControlDescriptor.java:266 - Completed population of control 'Control3 value: 'null'

2013-05-28 16:39:55,576 [http-8080-8 UserName] DEBUG ControlDescriptor.java:213 - Populating value of control Control4
//many lines of logging
2013-05-28 16:39:55,857 [http-8080-8 UserName] DEBUG ControlDescriptor.java:266 - Completed population of control 'Control4', value: 'null'

2013-05-28 16:39:59,482 [http-8080-8 UserName] DEBUG DynamicDialogService.java:417 - Got descriptors.  Building result object.

There are a few main components to the file:

"DynamicDialogService.getDialogDescriptors" designates the start of one action. There may be several of these in each file. The subsequent "controls" belong to this action.

        "ControlDescriptor.java:213" designates the start of a "control" with a name
        "ControlDescriptor.java:266" designates the end of a "control" with a matching name

        Subtracting the start timestamp from the end timestamp gives the total time elapsed for that control

"Got descriptors.  Building result object." designates the end of an action. again, getting this timestamp for subtraction along with the start of the action gives a total time elapsed for that action.


Any help / code / suggestions on how to tackle this would be great.

Answers (4)