Advertisement
parabola949

Network Logging C#

Jun 24th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.IO;
  3. using System.Net.NetworkInformation;
  4. using System.Threading;
  5.  
  6. namespace Network_Uptime_Logging
  7. {
  8.     class Program
  9.     {
  10.         static DateTime change = DateTime.Now;
  11.         static bool up = true;
  12.         static bool wasUp = true;
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             new StreamWriter("c:\\netup.txt", true).WriteLine(change.ToString() + ":\t" + ((up) ? "System Up" : "System Down"));
  17.             new System.Media.SoundPlayer("c:\\windows\\media\\Speech " + (up ? "On" : "Off") + ".wav").Play();
  18.             while (true)
  19.             {
  20.                 int pingTime = 0;
  21.                 pingTime = new ConnectedDelegate(IsConnectedToCharterDNS)();
  22.                 Console.Write(pingTime + ",");
  23.                 Thread.Sleep(TimeSpan.FromSeconds(5));
  24.             }
  25.         }
  26.  
  27.         delegate int ConnectedDelegate();
  28.  
  29.         public static int IsConnectedToCharterDNS()
  30.         {
  31.             string CharterDNS = "68.113.206.10";
  32.             int result = 0;
  33.             try
  34.             {
  35.                 PingReply reply = new Ping().Send(CharterDNS);
  36.                 if (reply.Status == IPStatus.Success)
  37.                     result = (int)reply.RoundtripTime;
  38.             }
  39.             catch { }
  40.             Log(DateTime.Now, result);
  41.             return result;
  42.         }
  43.  
  44.         public static void Log(DateTime time, int pingTime)
  45.         {
  46.             up = pingTime == 0 ? false : true;
  47.             if (up != wasUp && change < DateTime.Now.AddMinutes(-1))
  48.             {
  49.                 change = DateTime.Now;
  50.                 wasUp = up;
  51.                 new StreamWriter("c:\\netup.txt", true).WriteLine(change.ToString() + ":\t" + ((up) ? "System Up" : "System Down"));
  52.                 new System.Media.SoundPlayer("c:\\windows\\media\\Speech " + (up ? "On" : "Off") + ".wav").Play();
  53.             }
  54.             new StreamWriter("c:\\netup.csv",true).Write(time.ToString() + "," + pingTime.ToString() + Environment.NewLine);
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement