?
Solved

Parse Log Files in ASP.NET (Part 2)

Posted on 2011-10-19
3
Medium Priority
?
345 Views
Last Modified: 2012-05-12
At this point my code to pull a specifically formatted log file into my web app is successful. I can get it to display exactly as formatted in the log file.

My next issue is how can I cherry pick pieces of text in the log file and output them to a label control?

I pasted in the code window a sample of one of the log files I have to parse.

For Example I want to place "Microsoft® Windows Server® 2008 Enterprise" in a label on the page. How can I code this?

here is what my code looks like to collect the file:

'Open a file for reading
        Dim FILENAME As String = txtPath.Text & "\Results.log"
        'Get a StreamReader class that can be used to read the file
        Dim objStreamReader As StreamReader
        objStreamReader = File.OpenText(FILENAME)
        'Now, read the entire file into a string
        Dim contents As String = objStreamReader.ReadToEnd()

System Summary Information
===============================================================================================
OS Name					Microsoft® Windows Server® 2008 Enterprise 
OS Version				6.0.6002 Service Pack 2 Build 6002
OS Manufacturer				Microsoft Corporation
System Name				WPPEXMB08F
System Manufacturer			Dell Inc.
System Model				PowerEdge R900
System Type				x64-based PC

Open in new window

0
Comment
Question by:newimagent
  • 3
3 Comments
 
LVL 1

Author Comment

by:newimagent
ID: 36997709
The code I am trying to use returns bad file name or number exception on the 'LineInput(3).Substring(39)' section of code.

I'm not sure how I can define the file to make this work. I've tried multiple different formats but most of what I have found in searching doesn't deal with cherry picking specific characters out of the text file in VB. I've seen some posts with expressions but I would like to use VB as I'm new to this stuff and want to stay in VB as much as possible for right now.
Dim FILENAME As String = txtPath.Text & "\Results.log"
        'Validate the file exists
        If File.Exists(txtPath.Text & "\Results.log") Then
            MultiView1.ActiveViewIndex = 1
            'Get a StreamReader class that can be used to read the file
            Dim objStreamReader As StreamReader
            objStreamReader = File.OpenText(FILENAME)
            'Now, read the entire file into a string
            Dim contents As String = objStreamReader.ReadToEnd()
            Dim os As String = LineInput(3).Substring(39)

            txtOS.Text = os
            txtOut.Text = contents

Open in new window

0
 
LVL 1

Author Comment

by:newimagent
ID: 36997982
So apparently to use LineInput I have to use FileOpen. I still cannot figure out the proper context to collect just the text starting in row 3 with character 39 to the end of that row?

Any help would be greatly appreciated.
Dim os As String
            FileOpen(1, FILENAME, OpenMode.Input)
            While Not EOF(1)
                'Read line into variable
                os = LineInput(1).Substring(39)
                txtOS.Text = os
            End While

Open in new window

0
 
LVL 1

Accepted Solution

by:
newimagent earned 0 total points
ID: 36998114
Ok so I have figured it out. Hopefully this information is useful to someone because I could not find all these pieces in one spot anywhere. To read each line you need to define an array and read the file.

My finished working code is below. I'm not sure why the line and substring values are odd. it seems every line in the log file is a value of 2 in the data() array and i haven't figured out the substring value math either, but since the files i work with are formatted the same way even though the data filed may be different it still pulls all the data as expected. I imagine this might be an issue with more dynamic log content.

I can't find any closing statements for the IO.File.ReadAllLines() function so I'm assuming it doesn't require any? (an answer on that would be helpful)
Protected Sub btnGetDC_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGetDC.Click

        'Open a file for reading
        Dim FILENAME As String = txtPath.Text & "\Results.log"
        'Validate the file exists
        If File.Exists(FILENAME) Then
            MultiView1.ActiveViewIndex = 1
            'Get a StreamReader class that can be used to read the file
            Dim objStreamReader As StreamReader
            objStreamReader = File.OpenText(FILENAME)
            'Now, read the entire file into a string
            Dim contents As String = objStreamReader.ReadToEnd()
            txtOut.Text = contents
            'Close the stream when done so another operation can open it if necessary
            objStreamReader.Close()

            Dim data As String() = IO.File.ReadAllLines(FILENAME)
            Dim osName As String = data(6).Substring(25)
            Dim hostName As String = data(12).Substring(31)
            Dim osVer As String = data(8).Substring(29)

            txtOS.Text = osName
            txtHostName.Text = hostName
            txtOSVer.Text = osVer

        Else
            MultiView1.ActiveViewIndex = 0
            lblIsValid.Text = "* The path entered is not a vaild log directory"
        End If

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

840 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