C# 读取Log4net 日志文件

  开发过程当中为了调试和查找缘由,咱们常常会在程序中使用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,不然一直地市被占用          
相关文章
相关标签/搜索