開發過程當中爲了調試和查找緣由,咱們常常會在程序中使用log4net 寫入日誌,記錄執行過程,因此咱們每次找日誌的時候須要遠程登陸到服務器端而後使用文本工具打開查找內容。一臺服務器還好,若是要查找的是一個域名下的應用日誌,域名又綁定了不少臺ip,那找起來就很費時間了。爲此,作了一個小小的日誌查找工具。服務器
工具使用winform 寫的。支持日誌http 訪問和以共享目錄的方式訪問多線程
http訪問時:域名解析主要代碼工具
Uri uri = new Uri(txtAddress.Text); hostName = uri.Host; ips = System.Net.Dns.GetHostAddresses(hostName);
爲了加快查找速度,同時還使用了多線程處理,針對每一個ip開一個縣城單獨處理該ip上的應用日誌this
if (ips != null && ips.Length > 0) { foreach (IPAddress address in ips) { LogRequestParam param = new LogRequestParam(url, this.txtKeyWord.Text, address.ToString(), this.rbtnHttp.Checked ? 0 : 1, hostName,txtAddress.Text,this.txtUserName.Text,this.txtPassword.Text,txtFileName.Text); Thread thread = new Thread(new ParameterizedThreadStart(Search)); thread.Start(param); threads.Add(thread); } }
固然。在程序執行完畢給出適當的提示仍是有必要的,故在執行類中定義了兩個靜態變量url
/// <summary> /// 當前執行完成線程數量 /// </summary> private static int CurrentCount = 0; /// <summary> /// 總數量 /// </summary> private static int TotalCount = 0;
執行開始時TotalCount = threads.Count,CurrentCount = 0;spa
單個縣城執行完畢時CurrentCount++;線程
同時開闢另外一個線程去判斷是否執行完畢,並更新界面調試
List<int> ids=new List<int> (); while (CurrentCount != TotalCount) { foreach (Thread thread in threads) { if (!thread.IsAlive && !ids.Contains(thread.ManagedThreadId)) { ids.Add(thread.ManagedThreadId); CurrentCount++; } } }
共享目錄方式訪問時經過如下方式訪問共享目錄:日誌
public static bool connectState(string path, string userName, string passWord) { bool Flag = false; Process proc = new Process(); try { proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; proc.Start(); //string dosLine = @"net use " + path + " /User:" + userName + " " + passWord ; string dosLine = @"net use " + path; if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(passWord)) { dosLine = dosLine + " /User:" + userName + " " + passWord; } proc.StandardInput.WriteLine(dosLine); proc.StandardInput.WriteLine("exit"); while (!proc.HasExited) { proc.WaitForExit(1000); } string errormsg = proc.StandardError.ReadToEnd(); proc.StandardError.Close(); if (string.IsNullOrEmpty(errormsg)) { Flag = true; } } catch (Exception ex) { } finally { proc.Close(); proc.Dispose(); } return Flag; }
應爲共享基本都是使用內網共享的。因此須要將獲取的ip替換成內網ip,直接使用的是執行程序本機內網ip 的前三段+ip 的最後一段code
/// <summary> /// 使用IPHostEntry獲取本機局域網地址 /// </summary> /// <returns></returns> public static string GetLocalIp() { string hostname = Dns.GetHostName();//獲得本機名 IPHostEntry localhost = Dns.GetHostByName(hostname);//方法已過時,只獲得IPv4的地址 //IPHostEntry localhost = Dns.GetHostEntry(hostname); IPAddress localaddr = localhost.AddressList[localhost.AddressList.Length-1]; return localaddr.ToString(); }
而後直接像訪問本地文件同樣訪問文件
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, System.IO.FileShare.ReadWrite)) { using (StreamReader streamReader = new StreamReader(fileStream, System.Text.Encoding.Default)) { content = streamReader.ReadLine(); while (null != content) { if (content.Contains(param.KeyWord)) {
//邏輯
}
}
}
}
遇到的問題:1,在使用共享讀取當天文件時,因爲當天的文件一直被服務器佔用,在訪問時須要加上
System.IO.FileShare.ReadWrite,不然一直地市被佔用