dividing a text file depending on line into 2 different database tables
-
lundi 20 août 2012 20:17
Good evening, I have an application I have been working on for a week now and I have come to a road block with. I can't find much information regarding the situation I am having but I imagine that it has been accomplished, I may just not be looking in the right places.
Sample of text file...
<text>
[2012-07-04 23:56:07,320] [INFO ] [MessageManager] [1740] () [] [Starting ProcessReadMessageEvents]
[2012-07-04 23:56:08,007] [INFO ] [MessageManager] [7048] () [] [Starting ProcessReadMessageEvent]Exception: System.Net.WebException
[2012-07-04 23:56:08,007] [INFO ] [MessageManager] [7048] () [] [Starting ProcessReadMessageEvent]
Message: The remote server returned an error: (400) Bad Request.
Source: Common
at Common.CUMI.GetMessageById(String objectId, String msgId)
at Common.BizLogic.EventSyncLogic.GetMessageDetail(String loggerName, VoicemailMessageEvent vme, VoicemailSetting vm, LinkedUser ll)
at Common.BizLogic.EventSyncLogic.ProcessNewMessageEvent(VoicemailMessageEvent vme, LinkedUser ll, VoicemailSetting vms, EmailServer ems, String loggerName, DonomaLicense dl, DonomaSetting ds, List`1 msgs)
[2012-07-04 23:56:07,086] [INFO ] [MessageManager] [4304] () [] [There was no endpoint listening at http://172.30.29.145:7191/soap that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.]
Exception: System.ServiceModel.EndpointNotFoundException
Message: There was no endpoint listening at http://172.30.29.145:7191/soap that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Source: mscorlib
[2012-07-04 23:56:08,522] [INFO ] [MessageManager] [4224] () [] [Starting ProcessReadMessageEvent]</text>
My app is to open and read a .txt file. the code needs to go through it one line at a time to determine if it is a Log(Which is text with [ ]) or an Exception(which is Exception, Message, anything other that the Log[]). The Log Text line is divided up and sent to the Logger Table in MySQL database. and the idea is that when it reaches an Exception text it is to be divided up and sent to the Exception table. The exception text is linked to the last log text in the text file that is why I can not just load all of them at once but loop through them and when I come to an exception have a var to remember the current Log to go with the Exception. But ....
How do I loop through each line, realize there is an Exception Text then split the text to necessary columns and get the assigned object to assigned entity to put in database?? Thanks in advance
string[] lines = File.ReadAllLines(s); foreach (string line in lines) { if (line.Contains("[")) { var logQuery = from log in lines let logRecord = log.Split('[', ']', '(', ')') select new OLog() { OlogDate = logRecord[1], OlogLevel = logRecord[3], OlogLogger = logRecord[5], OlogThread = logRecord[7], OlogProperty = logRecord[9], OlogMethod = logRecord[11], OlogException = logRecord[12], }; foreach (var item in logQuery) { temp temp = new temp(); temp.logDate = item.OlogDate; temp.logLevel = item.OlogLevel; temp.logLogger = item.OlogLogger; temp.logThread = item.OlogThread; temp.logProperty = item.OlogProperty; temp.logMethod = item.OlogMethod; temp.logException = item.OlogException; } } else if (line.Contains("Exception:")) { var exQuery = from exMessage in lines let logRecord = exMessage.Split(':') select new ExTable() { ExlogException = logRecord[0], ExlogMessage = logRecord[1], ExlogSource = logRecord[3], ExlogServerStackTrace = logRecord[4], ExlogExRethrown = logRecord[5], }; foreach (var item in exQuery) { exception execp = new exception(); execp.LogException = item.ExlogException; execp.LogMessage = item.ExlogMessage; execp.LogSource = item.ExlogSource; execp.LogServerStackTrace = item.ExlogServerStackTrace; execp.LogExRethrown = item.ExlogRethrown;
}
}
}
- Modifié Martin1987 mardi 21 août 2012 13:57
Toutes les réponses
-
mercredi 22 août 2012 03:53
Hi Martin1987<abbr class="affil"></abbr>,
It is rather unclear from your post what seems to be the problem?
-
mercredi 22 août 2012 12:13
I am sorry, I ran this code and it would just loop in the "if(line.contains("["))" it wouldn't continue to loop and check to see what the other lines started with or contained. I wasn't sure if there was a better way of going about it?
I did manage to figure out my problem, which was I was completely off on the code I kept thinking I needed a linq query and I didn't. Here is the final solution that is looping as it is suppose to, as of now I just need to add a string builder to hold the exception lines as it loops through them.
Thank you for telling me I was unclear in my question.
foreach (string s in filePaths) { string[] lines = System.IO.File.ReadAllLines(s); { var counter = 0; foreach (string item in lines) { counter++; if (item.Contains("[")) { var temps = new temp(); temps.logDate = item.Split('[', ']')[1]; temps.logLevel = item.Split('[', ']')[3]; temps.logLogger = item.Split('[', ']')[5]; temps.logThread = item.Split('[', ']')[7]; temps.logProperty = item.Split('[', ']', ')', '(')[9]; temps.logMethod = item.Split('[', ']', ')', '(')[11]; temps.logException = item.Split('[', ']', ')', '(')[12]; currentLine = temps; logEntity.temps.AddObject(temps); dataGridView1.DataSource = (temps); } else if (item.Contains("Exception:")) { StringBuilder builder = new StringBuilder(); var tempitem = item; var eCounter = 0; tempExcept=new tempexception(); do { if (eCounter == 0) { tempExcept.LogException = tempitem; //builder.Append(i.ToString()); } eCounter++; tempitem = lines[counter]; } while (!tempitem.Contains("[")); } } logEntity.SaveChanges(); } }
- Modifié Martin1987 mercredi 22 août 2012 12:13
- Marqué comme réponse Martin1987 mercredi 22 août 2012 12:58
-
jeudi 23 août 2012 01:15Modérateur
Good job , Martin !
Glad to know you have solved this problem and thanks for sharing the solution.
Have a nice day !
Regards ,
Lisa Zhu [MSFT]
MSDN Community Support | Feedback to us

