最近一项目windows平台,需要针对 Serv-U ftp服务器进行实时监控,从而得知上传的成功后的文件信息,并且通过post提交给php进行分析处理
所以最后决定采用c#编写一个针对ftp日志文件的实时监控的windows服务,通过监控serv-u设置的日志文件大小变化进行分析,并且得到上传成功的文件路径
windows服务编写还好,就是调试有些困难,很少用c#.代码写得还是得简陋了些
主要功能:
xml格式的配置文件,在配置文件中指定ftp日志文件根目录,http请求地址及监控日志存放位置
按日期生成监控系统日志
主要代码
C#代码
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Linq;
- using System.ServiceProcess;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.Configuration;
- using System.Collections.Specialized;
- namespace ftpScan
- {
- public partial class Service1 : ServiceBase
- {
- System.Timers.Timer timer2 = new System.Timers.Timer(4500);
- public Service1()
- {
- InitializeComponent();
- }
- public static string logpath = SystemConfig.GetConfigData("logpath", string.Empty);
- public static string httpurl = SystemConfig.GetConfigData("httpurl", string.Empty);
- public static string ftppath = SystemConfig.GetConfigData("ftppath", string.Empty);
- public static string ScanLogPath = SystemConfig.GetConfigData("ScanLogPath", string.Empty);
- public static string s1 = " Received file ";
- public static string s2 = " successfully ";
- public static long count = 0;
- public static long size=0;
- public void log(string msg)
- {
- System.DateTime currentTime = new System.DateTime();
- currentTime = System.DateTime.Now;
- string strFilePath =ScanLogPath + "ftpScan_" + currentTime.ToString("yyyy_MM_dd") + ".log";
- if (!File.Exists(strFilePath))
- {
- FileStream fs = new FileStream(strFilePath, FileMode.Create); //实例化一个StreamWriter–>与fs相关联
- StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
- sw.WriteLine(currentTime.ToString() + " | " + msg);
- sw.Close();
- fs.Close();
- }
- else
- {
- FileStream fs = new FileStream(strFilePath, FileMode.Append);
- StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
- sw.WriteLine(currentTime.ToString() + " | " + msg);
- sw.Close();
- fs.Close();
- }
- }
- public void http(string dir, string filename)
- {
- string url = httpurl + "dir/" + dir + "/file/" + filename;
- log("提交http处理 目录:" + dir + " 文件名:" + filename);
- try
- {
- System.Net.WebClient wc = new WebClient();
- NameValueCollection na = new NameValueCollection();
- na.Add("dir",dir);
- na.Add("file",filename);
- wc.UploadValues(httpurl, "POST", na);
- // log(ss.ToString());
- //wc.DownloadData(url);
- }
- catch
- {
- log("url:" + url + " 无效!");
- }
- }
- public void scan()
- {
- System.DateTime currentTime = new System.DateTime();
- currentTime = System.DateTime.Now;
- char[] s3 = s1.ToCharArray();
- long count2 = count;
- string logfile = logpath + "ftp_" + currentTime.ToString("yyyy_MM_dd") + ".log";
- //Console.WriteLine(logfile);
- if (!File.Exists(logfile))
- {
- log("今日日志文件 " + logfile + " 不存在,已成功创建空的今日日志文件!");
- FileStream fs = new FileStream(logfile, FileMode.Create); //实例化一个StreamWriter–>与fs相关联
- fs.Close();
- }
- else
- {
- System.IO.FileInfo f = new FileInfo(logfile);
- //MessageBox.Show(f.Length.ToString());
- if (f.Length != size)
- {
- StreamReader sr = new StreamReader(logfile);
- string content = sr.ReadToEnd();
- string[] arr = content.Split("".ToCharArray());
- sr.Close();
- if (arr.Length > count)
- {
- long n = 0;
- foreach (string re in arr)
- {
- string s = re;
- if (n >= count)
- {
- //Console.WriteLine(s);
- if (s.Contains(s1) && s.Contains(s2))
- {
- s = s.Replace(s1, "|");
- s = s.Replace(s2, "|");
- Console.WriteLine(s);
- string[] ary = s.Split("|".ToCharArray());
- s = ary[1];
- s = s.Replace(ftppath, "");
- string[] ary2 = s.Split("\\".ToCharArray());
- if (ary2.Length == 2)
- {
- this.http(ary2[0], ary2[1]);
- }
- }
- }
- n++;
- }
- }
- else
- {
- long n = 0;
- foreach (string re in arr)
- {
- string s = re;
- //Console.WriteLine(s);
- if (s.Contains(s1) && s.Contains(s2))
- {
- s = s.Replace(s1, "|");
- s = s.Replace(s2, "|");
- Console.WriteLine(s);
- string[] ary = s.Split("|".ToCharArray());
- s = ary[1];
- s = s.Replace(ftppath, "");
- string[] ary2 = s.Split("\\".ToCharArray());
- if (ary2.Length == 2)
- {
- this.http(ary2[0], ary2[1]);
- }
- }
- n++;
- }
- }
- log("监控到了更新,原行:" + count.ToString() + " 原大小:" + size.ToString() + " 【新行:" + arr.Length.ToString() + " 新大小:" + f.Length.ToString() + "】");
- count = arr.Length;
- size = f.Length;
- }
- }
- //Contains //查找字符串
- }
- protected override void OnStart(string[] args)
- {
- //log("启动监控ftp日志服务!");
- //Process.Start("D://test//scoutFolder.exe");
- log("启动监控ftp日志服务!");
- timer2.Elapsed += new System.Timers.ElapsedEventHandler(theout);
- timer2.Enabled = true;
- }
- protected override void OnStop()
- {
- timer2.Enabled = false;
- log("停止监控ftp日志服务!");
- }
- public void theout(object source, System.Timers.ElapsedEventArgs e)
- {
- scan();
- }
- }
- }
这个blog界面我喜欢~~~
敢问是sablog自己的模板吗