繁体   English   中英

C#:行= StreamReader.ReadLine <=项目

[英]C# : line = StreamReader.ReadLine <= item

读取格式如下的文件

2014/11/03 14:31:03     PID:8696        UUID:2ae855da-37d1-4a99-8510-27539afa09d0       Start   E:/I3/IC/Logs/2014-11-03/SIPEngine_2.ininlog
2014/11/04 00:00:01     PID:8696        UUID:2ae855da-37d1-4a99-8510-27539afa09d0       End     E:/I3/IC/Logs/2014-11-03/SIPEngine_2.ininlog

我已经在变量中删除了时间,但需要能够在文件中搜索小于或等于该值的任何内容

感谢@dbc

现在我需要按日期获取比赛

while (reader.next(msg, control))
{
  ActiveAttributesVect_t attribs = msg.get_active_context_attributes();
  ActiveAttributeValuesVect_t attribVals = msg.get_active_context_attribute_values();
  int numAttribs = ((attribs != null) && (attribVals != null)) ? Math.Min(attribs.Count, attribVals.Count) : 0;
  for (int i = 0; i < numAttribs; ++i)
  {
    if (attribVals[i].ToString().Contains(strCallID))
    {
      callidList.Add(msg.expand_format_message());
      // Setting the date to match the log format
      strCallDate = msg.timestamp().as_creator_time(header.tz_offset()).ToString().Substring(0, 10).Replace("-", "/");
      strCallTime = msg.timestamp().as_creator_time(header.tz_offset()).ToString().Substring(11, 8);



                    Console.WriteLine("Call Time : {0}", strCallTime);
                    Console.WriteLine("Call Date : {0}", strCallDate);
                    Console.WriteLine("");
                    foreach (var entry in callidList)
                    {
                        Console.WriteLine(entry);
                    }
                    Console.WriteLine("");


                    OutputLogLinesBeforeTime(strLogDirectory, msg.timestamp().as_creator_time(header.tz_offset()), strCallTime);

                }
            }

您可以使用TimeSpan提取并解析一天中的某个时间:

    static TimeSpan? ExtractTime(string logLine)
    {
        var tokens = logLine.Split(new char [] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        if (tokens.Length < 2)
            return null;
        TimeSpan time;
        if (!TimeSpan.TryParse(tokens[1], out time))
            return null;
        return time;
    }

    static DateTime? ExtractDate(string logLine)
    {
        var tokens = logLine.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        if (tokens.Length < 1)
            return null;
        DateTime date;
        if (!DateTime.TryParse(tokens[0], out date))
            return null;
        return date;
    }

    static void OutputLogLinesBeforeTime(string strLogDirectory, string strLogDate, string strCallTime)
    {
        try
        {
            var time = TimeSpan.Parse(strCallTime);  // Throws a format exception if invalid.

            DirectoryInfo d = new DirectoryInfo(strLogDirectory + "\\" + strLogDate + "\\");

            foreach (var file in d.GetFiles("*.ininlog_journal"))
            {
                try
                {
                    using (Stream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    using (StreamReader sReader = new StreamReader(stream))
                    {
                        foreach (var line in sReader.EnumerateLines().Where(l => ExtractTime(l) < time))
                            Console.WriteLine(line);
                    }
                }
                catch (UnauthorizedAccessException ae)
                {
                    Console.WriteLine(ae.Message);
                }
                catch (SystemException se)
                {
                    Console.WriteLine(se.Message);
                }
                catch (ApplicationException ape)
                {
                    Console.WriteLine(ape.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
        catch (UnauthorizedAccessException ae)
        {
            Console.WriteLine(ae.Message);
        }
        catch (SystemException se)
        {
            Console.WriteLine(se.Message);
        }
        catch (ApplicationException ape)
        {
            Console.WriteLine(ape.Message);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

为了方便起见,我提取了以下内容:

public static class TextReaderExtensions
{
    public static IEnumerable<string> ReadLines(this TextReader sReader)
    {
        if (sReader == null)
            throw new ArgumentNullException();
        string line;
        while ((line = sReader.ReadLine()) != null)
            yield return line;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM