List <string>返回System.String []而不是实际文本

时间:2016-10-28 08:55:22

标签: c# string list parsing logfile

这是我实际上要做的事情,我正在尝试使用列表和类而不是现在的方式来做同样的事情:

public ActionResult Testcsvbp()
    {
        string Workingfolder = System.Web.HttpContext.Current.Server.MapPath(@"~/Files");
        string FileNamer = DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString() + DateTime.Now.Day.ToString();
        ViewBag.Output = "No Test Data Available...........";
        ViewBag.Details = "No Details Avalable...........";

        // Output the start time.
        DateTime startTime = DateTime.Now;
        ViewBag.Output = "Program Start Time: " + startTime.ToString("T");

        // Read all log file lines into an array.           
        string[] lines = System.IO.File.ReadAllLines(Workingfolder + @"/access.log");

        // Build a data table for all the lines we decide to keep.
        DataTable dt = new DataTable();
        dt.Columns.Add("Count", typeof(long));
        dt.Columns.Add("IP_Address", typeof(string));
        dt.Columns.Add("IP_Address_Integer", typeof(long));


        // Make the IP_Address Column a primary key to speed up the process of searching the data table over and over.
        // Note: the DataTable type does not have the capability to be indexed.
        dt.PrimaryKey = new DataColumn[] { dt.Columns["IP_Address"] };


        // Iterate over the lines array.  Since this iteration will be expensive, we want to hunt out bad lines and append the good lines into a dataTable.
        for (int i = 0; i < lines.Length; i++)
        {

            // Create array for that line, splitting fields by sspaces.  From this point, much of our conditional logic will be specific array indexes.
            // This assumes that this program is only for schema used in the logs/access.log file.
            string[] lineArray = lines[i].Split(' ');

            // We don't want to use comment lines or data within the comment lines.  To avoid this, we'll assume a length of 21 items for lines[i].
            if (lines[i].Substring(0, 1) != "#" && lineArray.Length == 21)
            {

                // Isolate lines where the request was a GET protocol on port 80. Also eliminate IPs starting with 207.114 .
                if (lineArray[7] == "80" && lineArray[8] == "GET" && lineArray[2].Substring(0, 7) != "207.114")
                {


                    // Create datarow to add to data table container.
                    DataRow dr = dt.NewRow();
                    dr["Count"] = 1;
                    dr["IP_Address"] = lineArray[2];
                    dr["IP_Address_Integer"] = IPtoInt(lineArray[2]);

                    // Create duplicate search expression and check for duplicates.
                    string searchExpression = "IP_Address = '" + lineArray[2].ToString() + "'";
                    DataRow[] duplicateRow = dt.Select(searchExpression);

                    // Prevent duplicate rows for an IP address.  If a duplicate is fount, add 1 to the "Count" row.  Else, add the row.                    
                    if (duplicateRow.Length > 0)
                    {
                        int duplicateIndex = dt.Rows.IndexOf(duplicateRow[0]);
                        dt.Rows[duplicateIndex]["Count"] = int.Parse(dt.Rows[duplicateIndex]["Count"].ToString()) + 1;
                    }
                    else
                    {
                        dt.Rows.Add(dr);
                    }

                    // Have the data table accept all changes.
                    dt.AcceptChanges();

                }
            }
        }

        // Now sort the datatable by the IP Address integer representation.
        DataView dv = dt.DefaultView;
        dv.Sort = "Count desc, IP_Address_Integer desc";
        dt = dv.ToTable();

        // Create a string builder to contain the CSV file contents.
        StringBuilder sb = new StringBuilder();

        // Add column names as the first line.
        sb.Append("Count,IP_Address");

        // Add the data to subsequent lines
        ViewBag.Output = ""; //
        foreach (DataRow row in dt.Rows)
        {
            var fields = row["Count"] + ",\"" + row["IP_Address"] + "\"\n";
            var columns = row["Count"] + ",\"" + row["IP_Address"] + "<br>";//mbelcher
            sb.AppendLine(fields);
            ViewBag.Output += columns; //
        }

        // Write the CSV file to the file system.
        string SaveFilePath = Workingfolder + @"\IPaddressComplete" + FileNamer + ".csv";
        using (StreamWriter sw = new StreamWriter(SaveFilePath))
         {
            sw.Write(sb.ToString());
            ViewBag.Details = sb.ToString();//
            ViewBag.FileNamer = SaveFilePath;
        }

        ViewBag.LinkToFile = "<a target='_blank' href='http://logparser.lol.com/files/" + @"IPaddressComplete" + FileNamer + ".csv'>Download File</a>";

        // Output the start time.
        TimeSpan duration = endTime - startTime;
        Console.WriteLine("Program Duration: " + duration.Seconds.ToString() + " seconds");
        return View("Testcsvbp");
    }

IIS LOG FILE行示例:

#Software: Microsoft Internet Information Services 5.0
#Version: 1.0
#Date: 2010-08-12 00:00:01
#Fields: date time c-ip cs-username s-sitename s-computername s-ip s-port     cs-method cs-uri-stem cs-uri-query sc-status sc-win32-status sc-bytes cs-bytes time-taken cs-version cs-host cs(User-Agent) cs(Cookie) cs(Referer) 

2010-08-12 00:00:01 69.143.116.98 - W3SVC106 STREAM 207.22.66.152 80 GET /includes/scripts.js - 200 0 2258 381 94 HTTP/1.1 www.lol.com Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+6.0;+WOW64;+GoogleT5;+SLCC1;+.NET+CLR+2.0.50727;+Media+Center+PC+5.0;+.NET+CLR+3.5.30729;+.NET+CLR+3.0.30618;+.NET4.0C) - http://www.lol.com/

我开始编写下面的代码,我在视图中得到的是:

  1. System.String []
  2. System.String []
  3. 我可能错过了一些东西但现在确实如何用列表和类来完成我想要的东西。

            // List to contain rows we are going to use.
        List<LogParserModel> LogListParsed = new List<LogParserModel>();
    
        var logFile = System.IO.File.ReadAllLines(Workingfolder +     @"/accessShort.log");
        List<string> LogList = new List<string>();
        foreach (var s in logFile) LogList.Add(s);
        var LogListCount = LogList.Count();
        ViewBag.LogListCount = LogListCount;
    
        // This will go through each line of the log file that was loaded into list LogList
        // 1. We split this by spaces to delemite values.
        string LineItemCheck = "";
        int LineItemCount = 0;
    
        //for (var m = 0; m < LogList.Count; m++)
        foreach (string line in LogList)
        {
            LineItemCount++;
            //Split on space - need to do this then turn back into alist?
            LineItemCheck = line;//          LogList.ToString();
            LineItemCheck = LineItemCheck.Split(' ').ToString();
            if (LineItemCheck.Substring(0, 1) == "#")
            {
                // we remove this line from the collection.
                ViewBag.RemovedLines += "(" + LineItemCount + ")" + LineItemCheck + "<br>";
            }
            else
            {
                //process it and add to the final "List<LogParser> LogListParsed"
                ViewBag.AddedLines += "(" + LineItemCount + ")" + LineItemCheck.ToString() + "<br>";
            }
        }
        ViewBag.LASTLineItemChecked = LineItemCheck;
        ViewBag.TotalLinesProcessed = LineItemCount.ToString();
        return View("Testcsv");     
    }   
    

    这是我制作的课程;完全没有任何东西:

        public class LogParserModel
    {
        public long HostCount { get; set; }
    
        [Key]
        public string IPAddress { get; set; }
        public long IPfilter { get; set; }
    }   
    

    下面是使用方法进行解析。

        public class LogParser
    {
        //Method to return something here
    
        //Method to return something here
    
        public string[] GetParsedList(string[] SomeArray)
        {
            return SomeArray.ToArray();
        }
    
    }
    

2 个答案:

答案 0 :(得分:2)

LineItemCheck = LineItemCheck.Split(' ').ToString();部分中,您可以拆分字符串,然后返回一个数组。因此,如果你ToString()它,它将返回你的数组/列表的类型。

答案 1 :(得分:0)

尝试以下代码

foreach (string line in LogList)
        {
            LineItemCount++;
            //Split on space - need to do this then turn back into alist?
            string LineItem = LineItemCheck = line;

        if (LineItem.Substring(0, 1) == "#")
        {
            // we remove this line from the collection.
            ViewBag.RemovedLines += "(" + LineItemCount + ")" + LineItemCheck + "<br>";
        }
        else
        {
            //process it and add to the final "List<LogParser> LogListParsed"
            ViewBag.AddedLines += "(" + LineItemCount + ")" + LineItemCheck.ToString() + "<br>";
        }
    }