2016-10-28 66 views
0

這裏是我真正想要做的,我想使用的方式名單和類別,而不是它現在是做同樣的事情:列表<string>返回System.String [],而不是實際的文本

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日誌文件行示例:

#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 []

我可能失去了一些東西,但現在真的知道如何來完成我想要列表和類。

 // 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

嘗試哪裏是 '下面的代碼'? – SeM

+1

縮進代碼有時可以幫助 – Petaflop

+1

首先,請縮進代碼 - 如果您需要幫助,它會使其更易讀,其次它看起來好像您誤解了String.Split('')'的功能,您的變量「string LineItemCheck =」 「;''不會顯示你想要的這個'LineItemCheck = LineItemCheck.Split('').ToString();' –

回答

2

LineItemCheck = LineItemCheck.Split(' ').ToString();部分你已經分裂字符串,它返回一個數組。所以如果你ToString()它,它會返回你的數組/列表的類型。

+0

這是他的代碼問題,但這是一個解決方案?他應該怎麼做? – mybirthname

+0

解決方法是檢查該部分中的代碼邏輯。 – SeM

+0

我之前沒有提供足夠的信息,所以我更新了它,以及我已經完成的工作。 – mbdevlists

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>"; 
     } 
    } 
+0

我之前沒有提供足夠的信息,所以我更新了它,以及我已經完成的工作。 – mbdevlists

相關問題