C# 读取Log4net 日志文件

  开发过程中为了调试和查找原因,我们经常会在程序中使用log4net 写入日志,记录执行过程,所以我们每次找日志的时候需要远程登录到服务器端然后使用文本工具打开查找内容。一台服务器还好,如果要查找的是一个域名下的应用日志,域名又绑定了很多台ip,那找起来就很费时间了。为此,做了一个小小的日志查找工具。

     工具使用winform 写的。支持日志http 访问和以共享目录的方式访问

  http访问时:域名解析主要代码

    

  Uri uri = new Uri(txtAddress.Text);
  hostName = uri.Host;
  ips = System.Net.Dns.GetHostAddresses(hostName);

     为了加快查找速度,同时还使用了多线程处理,针对每个ip开一个县城单独处理该ip上的应用日志

    

        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);
      }
    }        

    当然。在程序执行完毕给出适当的提示还是有必要的,故在执行类中定义了两个静态变量

    

       /// <summary>
       /// 当前执行完成线程数量
    /// </summary>
    private static int CurrentCount = 0;
    /// <summary>
    /// 总数量
    /// </summary>
    private static int TotalCount = 0;                    

    执行开始时TotalCount = threads.Count,CurrentCount = 0;

    单个县城执行完毕时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 的最后一段

    

      /// <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,否则一直地市被占用
          
原文地址:https://www.cnblogs.com/jecob/p/5238883.html