private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { try { List <string> listHeader = new List <string>(); string _filename = Comm.GetAttributesByPath("/mxh:configuration/mxh:MBProperty/mxh:IIsWebServer[@Location ='/LM/W3SVC/" + Id + "']", "LogFileDirectory"); if (string.IsNullOrEmpty(_filename)) { _filename = Comm.GetIIsWebServiceAttributesByPath("LogFileDirectory"); } _filename = _filename + "\\W3SVC" + Id + "\\" + LogFile; FileStream fs = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader reader = new StreamReader(fs, System.Text.Encoding.Default); int fileIndex = 0; while (reader.Peek() > -1) { string line = reader.ReadLine(); if (string.IsNullOrEmpty(line)) { continue; } if (IsLogHeader(line)) { if (line.StartsWith("#Fields:")) { string temp = line.Substring(9).Trim(); string[] header = temp.Split(new char[] { ' ' }); listHeader.AddRange(header); fileIndex = listHeader.IndexOf("cs-uri-stem"); } } else { if ( !listHeader.Contains("date") || !listHeader.Contains("time") || !listHeader.Contains("cs-uri-stem") || !listHeader.Contains("time-taken") || !listHeader.Contains("cs-host") ) { continue; } string[] _fieldsItem = line.Split(new char[] { ' ' }); if (_fieldsItem[fileIndex].IndexOfAny(Path.GetInvalidPathChars()) == -1 && Path.HasExtension(_fieldsItem[fileIndex])) { string ext = Path.GetExtension(_fieldsItem[fileIndex]).Substring(1); if ("asp|asa|aspx|ascx|php|php2|php3|php4|php5".IndexOf(ext) > -1) { if (Convert.ToDecimal(_fieldsItem[listHeader.IndexOf("time-taken")]) > numericUpDown1.Value) { dtNow.Rows.Add( TimeZone.CurrentTimeZone.ToLocalTime(Convert.ToDateTime(_fieldsItem[listHeader.IndexOf("date")] + " " + _fieldsItem[listHeader.IndexOf("time")])), "http://" + _fieldsItem[listHeader.IndexOf("cs-host")] + _fieldsItem[listHeader.IndexOf("cs-uri-stem")], _fieldsItem[listHeader.IndexOf("time-taken")] ); } } } } } fs.Close(); reader.Close(); } catch (Exception ex) { dtNow.Rows.Add(null, null, null, ex.Message); } }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { Dictionary <string, string> days = new Dictionary <string, string>(); for (int i = 0; i < (dtp2.Value.Date - dtp1.Value.Date).Days + 1; i++) { days.Add(dtp1.Value.AddDays(i).ToString("yyyy-MM-dd"), dtp1.Value.AddDays(i).ToString("exyyMMdd") + ".log"); } int NowIndex = -1; string NowWeb_Name = new List <string>(NowWeb.Keys)[0]; string NowWeb_Id = NowWeb[NowWeb_Name]; foreach (var item in days) { NowIndex++; LogInfo _loginfo = new LogInfo(); _loginfo.OptTime = item.Key; _loginfo.Web = NowWeb_Name; try { List <string> listHeader = new List <string>(); string _filename = Comm.GetAttributesByPath("/mxh:configuration/mxh:MBProperty/mxh:IIsWebServer[@Location ='/LM/W3SVC/" + NowWeb_Id + "']", "LogFileDirectory"); if (string.IsNullOrEmpty(_filename)) { _filename = Comm.GetIIsWebServiceAttributesByPath("LogFileDirectory"); } _filename = _filename + "\\W3SVC" + NowWeb_Id + "\\" + item.Value; FileStream fs = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader reader = new StreamReader(fs, System.Text.Encoding.Default); int fileIndex = 0; while (reader.Peek() > -1) { string line = reader.ReadLine(); if (string.IsNullOrEmpty(line)) { continue; } if (IsLogHeader(line)) { if (line.StartsWith("#Fields:")) { string temp = line.Substring(9).Trim(); string[] header = temp.Split(new char[] { ' ' }); listHeader.AddRange(header); fileIndex = listHeader.IndexOf("cs-uri-stem"); } } else { if (!listHeader.Contains("cs-uri-stem") || !listHeader.Contains("sc-bytes") || !listHeader.Contains("cs-bytes") || !listHeader.Contains("sc-status")) { continue; } string[] _fieldsItem = line.Split(new char[] { ' ' }); double _sc_bytes = Convert.ToDouble(_fieldsItem[listHeader.IndexOf("sc-bytes")]); double _cs_bytes = Convert.ToDouble(_fieldsItem[listHeader.IndexOf("cs-bytes")]); if (_fieldsItem[fileIndex].IndexOfAny(Path.GetInvalidPathChars()) == -1 && Path.HasExtension(_fieldsItem[fileIndex])) { string ext = Path.GetExtension(_fieldsItem[fileIndex]).Substring(1); if (Ext_html.IndexOf(ext) > -1) { _loginfo.HTML += _sc_bytes; } else if (Ext_Dynamic.IndexOf(ext) > -1) { _loginfo.ASP += _sc_bytes; } else if (Ext_Compress.IndexOf(ext) > -1) { _loginfo.RAR += _sc_bytes; } else if (Ext_Picture.IndexOf(ext) > -1) { _loginfo.Pic += _sc_bytes; } else if (Ext_Music.IndexOf(ext) > -1) { _loginfo.Music += _sc_bytes; } else if (Ext_Movie.IndexOf(ext) > -1) { _loginfo.Movie += _sc_bytes; } if (_fieldsItem[listHeader.IndexOf("sc-status")] == "500") { _loginfo.ASP500 = _loginfo.ASP500 + 1; } _loginfo.Down += _sc_bytes; _loginfo.Up += _cs_bytes; } } } fs.Close(); reader.Close(); if (_loginfo.Down + _loginfo.Up == 0) { _loginfo.Remark = "您的IIS日志不信息不足,请先设置日志记录格式!"; } backgroundWorker1.ReportProgress(NowIndex * 100 / days.Count, _loginfo); } catch (Exception ex) { _loginfo.Remark = "错误:" + ex.Message; backgroundWorker1.ReportProgress(NowIndex * 100 / days.Count, _loginfo); } } }