5 Replies - 1463 Views - Last Post: 21 December 2013 - 07:49 AM Rate Topic: -----

#1 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

How to add log file content to data table

Posted 19 December 2013 - 04:14 AM

Hi,

I am trying to add the content of log files to data table using the following code



[code]string[] fileContents = null;                 
                 List<string> words = new List<string>();
                 List<string> words1 = new List<string>();
                 DataTable workTable = new DataTable();
                
                 try
                 {
                     // Open and read the entire Data Log File contents.
                     fileContents = File.ReadAllLines(file);
                 }
                 catch (Exception ex)
                 {
                     throw new Exception("The Input log file was not found or is not in correct format\nDetails: " + ex.Message);
                 }
 
                 if (fileContents == null)
                     throw new Exception("The Input log file was not found or is not in correct format");
 
                 // Process each line of the Data Log File and filter out the CAN Msg Id's corresponding to each CAN Bus.
                 for (int Index = 0; Index < fileContents.Length; Index++)
                 {
                     string CANMsgId = string.Empty;
                     string[] spaceSeperator = new string[] { " " };
                     string[] lineWords = (fileContents[Index].Trim()).Split(spaceSeperator, StringSplitOptions.RemoveEmptyEntries);
                     if (lineWords.Length < (2 + 1))
 
                         continue;
 
                     // If a CAN Msg Id is valid, it should end with 'x'. If it doesnot end with 'x',
                     // then skip the entry and go to the next line of log file
                     if (lineWords[2].EndsWith("x"))
                     {
                         int i = 1;
                         //CANMsgId = lineWords[2].TrimEnd('x');
                         //words.Add(lineWords[0]);
                         //words1.Add(lineWords[1]);
                         workTable.Columns.Add(lineWords[2].TrimEnd('x'));
                         //workTable.Columns.Add("CustFName", typeof(String));
                         workTable.Columns.Add(lineWords[0]);
 
                        i++;
                     }
                     
                 }



I am getting exception at the following line of code

workTable.Columns.Add(lineWords[2].TrimEnd('x'));


Exception:
System.Data.DuplicateNameException

I am attaching the log file with this message

Thanks
John

Is This A Good Question/Topic? 0
  • +

Replies To: How to add log file content to data table

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: How to add log file content to data table

Posted 19 December 2013 - 04:23 AM

Moved to the C# help forum. Please don't post standard questions in the Advanced Discussions sections.
Was This Post Helpful? 0
  • +
  • -

#3 Charles:)   User is offline

  • D.I.C Regular

Reputation: 149
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: How to add log file content to data table

Posted 19 December 2013 - 06:10 AM

You're trying to add two columns with the same name. It looks like you're looping through each line in the log file and adding a column to the table for the third word in each line if that word ends in x. I assume that you have the same CANMsgId on more than one line in your log file.

What you choose to do depends on how you want to handle the data, but you could just check the table's collection of columns to see whether you're trying to add a column that already exists.
Was This Post Helpful? 2
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How to add log file content to data table

Posted 19 December 2013 - 07:25 AM

Quote

fileContents = File.ReadAllLines(file);

What happens when this is a 2gb log file? It happens when logs just keep getting appended to for a month.
You might consider reading/adding one line at a time. That also lets you get 50% through a file successfully even if there is some corruption. The way you have this is very all or nothing.

Also, don't throw errors saying the file wasn't found unless the reason is really that it wasn't found. That's causes people to spend hours tracking down a reason for the wrong problem. If the file was found, but file could not be read, then say "Error reading file"

You might also consider future needs. Right now you just read the data to a table. What happens when you want to add filtering, sorting, grouping? You might want to read the data into a collection of Record objects, or LogEntry objects. Then you can work the collection in memory and only display what you need, the way the user wants it.
Was This Post Helpful? 3
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How to add log file content to data table

Posted 19 December 2013 - 12:47 PM

Quote

Also, don't throw errors saying the file wasn't found unless the reason is really that it wasn't found. That's causes people to spend hours tracking down a reason for the wrong problem. If the file was found, but file could not be read, then say "Error reading file"


Quoting for visibility. Please please please for the sake of the next programmer that has to work on your code, use descriptive error messages and logging. Please. I've chased my tail for days tracking down bugs completely unrelated to the error message they cause because lazy programmers decide that any old error is good enough. Or the dreaded "unexpected error" with no extra details...nightmarish flashbacks.
Was This Post Helpful? 3
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How to add log file content to data table

Posted 21 December 2013 - 07:49 AM

Side note:
Interesting that these two LogFile related threads were started so close together


Maybe the two OP's should get together and work with each other and learn from each other on a Logger class and display methodology they both like. It is a coding community after all. It would be a good experience at working with others and not coding in a vacuum.

This post has been edited by tlhIn`toq: 21 December 2013 - 07:49 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1