如何在记事本中读取数据时在c#中分割日期,时间和错误描述 [英] How to split date, time, and error description in c# while reading data in notepad

查看:72
本文介绍了如何在记事本中读取数据时在c#中分割日期,时间和错误描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我在记事本中维护错误日志。我正在阅读不以'at'开头的行,这样我将只得到以日期开头的行,但我必须将该行日期,时间和错误描述拆分并在datagridview中显示(gridview列是日期,时间,错误描述)。请有人帮助我。以下是样本日志



11/5/2013 6:19:05 PM:无效的对象名称'batchresult'。 - 在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection)

在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection)

在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)

在System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)<在System.Data.SqlClient.SqlDataReader.ConsumeMetaData()的


在System.Data.SqlClient.SqlDataReader.get_MetaData()

在System。 Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)

在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async)

t.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 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,String method)
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)

在System.Data.Common.DbDataAdapter.FillInternal(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命令,CommandBehavior行为)

在System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)

在General_Invoice.Classes.Cls_DB_Con.GetArrayList(字符串查询)中的D:\ TCSINV \ 04开发\ Branch \ TCS_SQL_CODE \General_Invoice\Classes \DatabaseAccess.cs:第254行

11/6/2013 6:26:25 PM:长度不能小于零。

参数name:length - 在System.String.InternalSubStringWithChecks(Int32 startIndex,Int32 length,Boolean fAlwaysCopy)

在System.String.Substring(Int32 startIndex,Int32 length)

at D:\ TCSINV \ 04中的General_Invoice.BatchProcess.LoadTabularGrid(String CurrentSelectedBatchID,String TreeSelectedFileName)Development\Branch\TCS_SQL_CODE\General_Invoice\UserInterface\BatchProcess.cs:line 686

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

推荐答案

你可以使用字符串处理函数来完成这个。



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



s你可以操作时间和错误/异常消息的方式。
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.


通常我会提倡使用String.Split方法 [ ^ ]但这里不能使用,因为消息的部分的分隔符实际上构成了这些部分的一部分(例如冒号:是约会的一部分,也是终止它的时候)



所以我们必须使用我们对字符串格式的了解 - 有一些警告!!

- 如果日期是11/10/2013​​怎么办 - 10个字符而不是你的例子中的9个

- 如果时间是11: 19:05 PM - 11个字符而不是10个



我们所知道的是日期是由第一个空格终止的,并且时间由AM:或PM:终止



所以以下方法可行...

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)


这篇关于如何在记事本中读取数据时在c#中分割日期,时间和错误描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆