博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 读取Log4net 日志文件
阅读量:4508 次
发布时间:2019-06-08

本文共 4443 字,大约阅读时间需要 14 分钟。

  开发过程中为了调试和查找原因,我们经常会在程序中使用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);      }    }

 

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

    

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

 

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

    单个县城执行完毕时CurrentCount++;

    同时开辟另一个线程去判断是否执行完毕,并更新界面

    List
ids=new List
();    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 的最后一段

    

      ///         /// 使用IPHostEntry获取本机局域网地址        ///         /// 
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

你可能感兴趣的文章
centos7下安装python3
查看>>
服务器监控之 Monitorix 初体验
查看>>
Win10安装和配置JDK
查看>>
国内物联网平台(2):阿里云物联网套件
查看>>
btcpool之StratumServer
查看>>
TP4056大电流1A使用注意事项
查看>>
thinkphp5.0 + 微信分享
查看>>
Android USB通信-实现lsusb
查看>>
Ubuntu14.04搭建Caffe(仅CPU)
查看>>
Android(java)学习笔记85:使用SQLite的基本流程
查看>>
Java基础知识强化102:线程间共享数据
查看>>
fun() 的 拆分和 for 遍历 的结合---------> 函数容器
查看>>
Asp.Net4.5 mvc4(二) 页面创建与讲解
查看>>
java中字节数组怎么转换为无符号整数
查看>>
C Run-Time Error R6034问题的解决
查看>>
给自己的博客领养一些小宠物--增加趣味性的小插件 ...
查看>>
JS——事件详情(鼠标事件:clientX、clientY的用法)
查看>>
SpringBoot整合JavaWeb
查看>>
talk is cheap, show me the code——dcgan,wgan,wgan-gp的tensorflow实现
查看>>
GNOME 3.7.1 会有什么新功能呢?
查看>>