Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3792396
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:36:57+00:00 2026-05-19T12:36:57+00:00

I have a log file that I want to parse and load into a

  • 0

I have a log file that I want to parse and load into a database. I’m struggling with the best way to go about parsing it.

The log file is in the format Category: Information

Case Number: CASE01  
User ID: JOSM  
Software: Microsoft Word  
Date Started: 21-01-2010  
Date Ended: 22-01-2010  

Thing is, there’s other bits and pieces thrown into the log file that mean the information isn’t always present on the same line. I also only want the information, not the category.

So far, I’ve tried stick it all into an array separated by \r\n, but I have to know the index of the information I want in order to consistently retrieve it, and that changes. I’ve also tried feeding it through StreamReader and saying

if (line.Contains("Case Number"))
{
    tbReport.AppendText("Case Number: " + line.Remove(0, 13) + "\r\n");
}

Which gets me the information I want, but makes it very hard to do anything with.

I feel I’m better off going down the array path, but I could do with some guidance on how to search the array for the the category, and then parse the information.

Once I can parse it accurately, adding it into a database should be fairly straight forward. As it’s my first time attempting this, I’d be interested in any tips or guidance as to the best way to go about this though.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:36:58+00:00Added an answer on May 19, 2026 at 12:36 pm

    This will give you a collection with all key/value pairs.

    List<KeyValuePair> items = new List<KeyValuePair>();
    
    var line = reader.ReadLine();
    while (line != null)
    {
        int pos = line.IndexOf(':');
        items.Add(new KeyValuePair(line.Substring(0, pos), line.Substring(pos+1));
    
        line = reader.ReadLine();
    }
    

    If you have a log class which contains all possible names as properties, you can use reflection instead:

    class LogEntry
    {
        public string CaseNumber { get; set; }
        public string User { get; set; }
        public string Software{ get; set; }
        public string DateStarted { get; set; }
        public string DateEnded { get; set; }
    }
    
    List<LogEntry> items = new List<LogEntry>();
    
    var line = reader.ReadLine();
    var currentEntry = new LogEntry();
    while (line != null)
    {
        if (line == "") //empty line = new log entry. Change to your delimiter.
        {
             items.Add(currentEntry);
             currentEntry = new LogEntry();
        }
    
        int pos = line.IndexOf(':');
        var name = line.Substring(0, pos).Replace(" ", string.Empty);
        var value = line.Substring(pos+1);
    
        var pi = entry.GetType().GetProperty(name);
        pi.SetValue(entry, value, null);
    
    
        line = reader.ReadLine();
    }
    

    Note that I’ve not tested the code (just written it directly in here). You have to add error checking and such. The last alternative is not very performant as it is, but should do OK.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Perl newbie here. I have a log file that I need to parse out
I want to parse a timestamp from a log file that has been written
I have a log file that can get pretty big. The information in my
I have a log file that has a lot of tagging information, i.e, ERROR,
I have constructed an XSL file that parses an XML formatted log and generates
I have a log file full of queries, and I only want to see
I have a file that I'm using to log IP addresses for a client.
i have a log file which contains hundreds/thousands of seperate XML messages and need
Advance New Year Wishes to All. I have an error log file with the
I have to develop an application which parses a log file and sends specific

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.