Click here to Skip to main content
15,913,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project i am maintaining error logs in notepad. I am reading lines which not starts with 'at' so that i will get only line which starts with date but i have to split that line date, time and error description and display it in datagridview (gridview columns are Date, Time ,Error Description). please anyone help me. below are sample log

11/5/2013 6:19:05 PM:Invalid object name 'batchresult'. - at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
at General_Invoice.Classes.Cls_DB_Con.GetArrayList(String Query) in D:\TCSINV\04 Development\Branch\TCS_SQL_CODE\General_Invoice\Classes\DatabaseAccess.cs:line 254
11/6/2013 6:26:25 PM:Length cannot be less than zero.
Parameter name: length - at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at General_Invoice.BatchProcess.LoadTabularGrid(String CurrentSelectedBatchID, String TreeSelectedFileName) in D:\TCSINV\04 Development\Branch\TCS_SQL_CODE\General_Invoice\UserInterface\BatchProcess.cs:line 686
Posted

You can use string handling functions to get this done.

Date - Line1.Substring(0,9).Trim()

The same way you can manipulate for time and error/exception message.
 
Share this answer
 
Normally I would advocate the use of the String.Split Method[^] but that can't be used here as the delimiters for the "sections" of the message actually form part of those sections (e.g. colon : is part of the date as well as terminating it)

So we have to use what we know about the format of the string - with some caveats!!
-- What if the date is "11/10/2013" - 10 characters long instead of the 9 in your example
-- What if the time is "11:19:05 PM" - 11 characters long instead of 10

What we do know is that the date is terminated by the first space, and the time is terminated by either "AM:" or "PM:"

So the following would work ...
string s = "11/5/2013 6:19:05 PM:Invalid object name 'batchresult'. - at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)";

// Date is terminated by a space
string d = s.Substring(0, s.IndexOf(' ')).Trim();

// Time is the next section
string t = s.Substring(d.Length + 1);
// can't look for : as it's the separator for time formats but M: will terminate it
t = t.Substring(0, t.IndexOf("M:") + 1).Trim();

// Anything left is the rest of that message line
string r = s.Substring(s.IndexOf("M:") + 2);    // added 2 because my delimiter is 2 chars

//using System.Diagnostics;
Debug.Print(s);
Debug.Print(d);
Debug.Print(t);
Debug.Print(r);
DateTime d1;

// This bit was just to prove I found the date correctly
bool b = DateTime.TryParse(d, out d1);
Debug.Print(d1.ToString());

The above code produced the output
11/5/2013
6:19:05 PM
Invalid object name 'batchresult'. - at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900