How to Read dates in text file using C# -


i writing logs in text file @ local location(c:\temp\log.txt) using c#. text file stored below

 2011-11-17 23:05:17,266 [6] fatal application   2011-11-17 23:05:18,094 [6] fatal service   2011-11-17 23:17:08,862 [6] fatal receipts - savereceipts  system.invalidoperationexception: sequence contains no elements  @ system.linq.enumerable.first[tsource](ienumerable`1 source)  @ gid.aaframework.entitywrapper.receiptfacade.savereceipts(ienumerable`1 records,     string user) in     c:\advancedanalyticsprojects\surya\trunk\dotnet_src\gid.aaframework.entitywrapper\receiptfacade.cs:line 632 

now want read file , want take first time log entered date , last date

how can first time date , last updated date in text file?

now reading text file using following code:

streamreader sr = new streamreader(filelocation); if (sr != null) {   linelist.add(sr.readtoend());   loginfobydate.add(filelocation, "startdate:" + linelist.first().substring(0, 10) + "|" + "enddate:" + linelist.last().substring(0, 10));                } 

this code write taking first time date , last updated date if exception line single not working exception multiple lines show above. problem.can tell me how take first , last date in text file?

here example how parse this:

//init datetime list log entries list<datetime> logdates = new list<datetime>();  //define regex string string pattern = @"(?<logdate>(\d){4}-(\d){2}-(\d){2}\s(\d){2}:(\d){2}:(\d){2})"; regex reg = new regex(pattern);  //read log content string logcontent = file.readalltext("test.log");  //run regex matchcollection matches = reg.matches(logcontent);   //iterate on matches foreach (match m in matches) {     datetime logtime = datetime.parse(m.groups["logdate"].value);     logdates.add(logtime); }  //show first , last entry console.writeline("first: " + logdates.first()); console.writeline("last: " + logdates.last()); 

i've removed comma milliseconds make easier parse.

regards florian


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -