event log C# .net Project Logs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Data.OleDb;
using System.Data;
namespace Logs
{
class Program
{
static void Main(string[] args)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Directory.GetCurrentDirectory() + "/db1.mdb;");
login:
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("UserName:");
string userName = Console.ReadLine();
Console.Write("password:");
string p ="";
while (true)
{
ConsoleKeyInfo ki = new ConsoleKeyInfo();
ki=Console.ReadKey();
if (ki.Key==ConsoleKey.Enter)
{
break;
}
p +=ki.KeyChar.ToString();
Console.Write(Convert.ToChar(8));
Console.Write("*");
}
Console.WriteLine();
OleDbDataAdapter od = new OleDbDataAdapter("select * from t1 where username='" + userName + "' AND password='" + p + "'", con);
DataTable dt = new DataTable();
od.Fill(dt);
if (dt.Rows.Count != 1)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Invalid userName or Password");
Console.WriteLine();
Console.WriteLine();
goto login;
}
if (dt.Rows[0][4].ToString() == "Admin")
{
top:
Console.ResetColor();
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.Title = "<<<< Welcome To secure world >>>> Administrator";
Console.WriteLine("**** Select a option ****");
Console.WriteLine("1.Write Logs");
Console.WriteLine("2.Read Logs");
Console.WriteLine("3.Settings");
Console.WriteLine("4.Exit");
Console.Write("enter your choice:");
string choice;
try
{
choice = Console.ReadLine();
if (!(choice == "1" || choice == "2" || choice == "3" || choice == "4"))
{
Console.Clear();
Console.WriteLine("*** invalid f choice ***");
goto top;
}
}
catch
{
Console.Clear();
Console.WriteLine("*** invalid choice ***");
goto top;
}
if (choice == "1")
{
for (int d = 0; d < 8; d++)
{
try
{
Console.BackgroundColor = ConsoleColor.Blue;
EventLog eventLog1 = new EventLog();
if (d == 0)
{
eventLog1.Log = "System";
}
else if (d == 1)
{
eventLog1.Log = "Windows PowerShell";
}
else if (d == 2)
{
eventLog1.Log = "Application";
}
else if (d == 3)
{
eventLog1.Log = "HardwareEvents";
}
else if (d == 4)
{
eventLog1.Log = "Internet Explorer";
}
else if (d == 5)
{
eventLog1.Log = "Key Management Service";
}
else if (d == 6)
{
eventLog1.Log = "Media Center";
}
else if (d == 7)
{
eventLog1.Log = "Security";
}
eventLog1.MachineName = Environment.MachineName;
Console.WriteLine();
Console.WriteLine("Current MachineName: " + Environment.MachineName);
Console.WriteLine("Requesting for "+eventLog1.Log+"Logs...");
Thread.Sleep(1000);
Console.WriteLine(" Logs Found(" + eventLog1.Entries.Count + ")");
Console.BackgroundColor = ConsoleColor.Magenta;
Console.WriteLine("Waiting for system-Response...");
Thread.Sleep(1000);
foreach (EventLogEntry entry in eventLog1.Entries)//to create directory
{
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\" + Environment.MachineName + "\\" + eventLog1.Log + "\\" + entry.EntryType.ToString()))
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\" + Environment.MachineName + "\\" + eventLog1.Log + "\\" + entry.EntryType.ToString());
}
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\" + Environment.MachineName + "\\" + eventLog1.Log + "\\" + entry.EntryType.ToString() + "\\" + entry.Source))
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\" + Environment.MachineName + "\\" + eventLog1.Log + "\\" + entry.EntryType.ToString() + "\\" + entry.Source);
}
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\" + Environment.MachineName + "\\" + eventLog1.Log + "\\" + entry.EntryType.ToString() + "\\" + entry.Source + "\\" + entry.TimeWritten.ToLongDateString()))
{
// string sp = AppDomain.CurrentDomain.BaseDirectory + "\\"+Environment.MachineName+"\\" + entry.EntryType.ToString() + "\\" + entry.Source + "\\" + entry.TimeWritten.ToLongDateString();
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\" + Environment.MachineName + "\\" + eventLog1.Log + "\\" + entry.EntryType.ToString() + "\\" + entry.Source + "\\" + entry.TimeWritten.ToLongDateString());
}
if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\" + Environment.MachineName + "\\" + eventLog1.Log + "\\" + entry.EntryType.ToString() + "\\" + entry.Source + "\\" + entry.TimeWritten.ToLongDateString() + "\\" + entry.Index.ToString() + ".log"))
{
string s1 = entry.Message, s2 = "";
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("\nLog Index=" + entry.Index + " Encripting...");
for (int i = 0; i < s1.Length; i++)
{
int value = Convert.ToInt32(s1[i]) / 16;
int remaim = Convert.ToInt32(s1[i]) % 16;
if (remaim > 9)
{
if (remaim == 10)
{
s2 += value.ToString() + "A";
}
else if (remaim == 11)
{
s2 += value.ToString() + "B";
}
else if (remaim == 12)
{
s2 += value.ToString() + "C";
}
else if (remaim == 13)
{
s2 += value.ToString() + "D";
}
else if (remaim == 14)
{
s2 += value.ToString() + "E";
}
else if (remaim == 15)
{
s2 += value.ToString() + "F";
}
}
else
{
s2 += value.ToString() + remaim.ToString();
}
s2 += " ";
}
File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "\\" + Environment.MachineName + "\\" + eventLog1.Log + "\\" + entry.EntryType.ToString() + "\\" + entry.Source + "\\" + entry.TimeWritten.ToLongDateString() + "\\" + entry.Index.ToString() + ".log", s2);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write(" Successed");
}
}
}
catch { }
}
}
else if (choice == "2")
{
Console.ResetColor();
Console.Clear();
Console.ForegroundColor = ConsoleColor.DarkRed;
string[] EntryTypeList = Directory.GetDirectories(AppDomain.CurrentDomain.BaseDirectory + "\\" + Environment.MachineName);
Console.WriteLine("**** Select logType ****");
for (int i = 0; i < EntryTypeList.Length; i++)
{
DirectoryInfo di = new DirectoryInfo(EntryTypeList[i]);
Console.WriteLine(i + 1 + "." + di.Name);
}
Console.Write("enter your choice:");
choice = Console.ReadLine();
Console.Clear();
Console.WriteLine("**** Select LogProvider ****");
string[] logen = Directory.GetDirectories(EntryTypeList[int.Parse(choice) - 1]);
for (int i = 0; i < logen.Length; i++)
{
DirectoryInfo di = new DirectoryInfo(logen[i]);
Console.WriteLine(i + 1 + "." + di.Name);
}
Console.Write("enter your choice:");
choice = Console.ReadLine();
Console.Clear();
Console.WriteLine("**** Select LogProvider ****");
string[] logSource = Directory.GetDirectories(logen[int.Parse(choice) - 1]);
for (int i = 0; i < logSource.Length; i++)
{
DirectoryInfo di = new DirectoryInfo(logSource[i]);
Console.WriteLine(i + 1 + "." + di.Name);
}
Console.Write("enter your choice:");
choice = Console.ReadLine();
Console.Clear();
Console.WriteLine("**** Select Log-Index ****");
Console.WriteLine("index\t\tDate");
string[] logDate = Directory.GetDirectories(logSource[int.Parse(choice) - 1]);
Console.ForegroundColor = ConsoleColor.Red;
for (int i = 0; i < logDate.Length; i++)
{
DirectoryInfo di = new DirectoryInfo(logDate[i]);
string[] logIndex = Directory.GetFiles(logDate[i]);
for (int j = 0; j < logIndex.Length; j++)
{
FileInfo fi = new FileInfo(logIndex[j]);
Console.WriteLine(fi.Name.Remove(fi.Name.IndexOf(".log")) + "\t" + di.Name);
}
}
Console.Write("enter Index-Number:");
string fileName = Console.ReadLine();
for (int i = 0; i < logDate.Length; i++)
{
if (File.Exists(logDate[i] + "\\" + fileName + ".log"))
{
string FileData = File.ReadAllText(logDate[i] + "\\" + fileName + ".log");
string getedData = "";
int count = 0;
for (int r = 0; r < FileData.Length; r++)
{
if (FileData[r].ToString() == " ")
{
count++;
}
}
string[] w_list = new string[count];
int w_index = 0;
for (int r = 0; r < FileData.Length; r++)
{
if (FileData[r].ToString() == " ")
{
w_index++;
}
else
{
w_list[w_index] += FileData[r].ToString(); ;
}
}
for (int ii = 0; ii < w_list.Length; ii++)
{
int w = 0;
if (w_list[ii][w_list[ii].Length - 1].ToString() == "A")
{
w = 10;
}
else if (w_list[ii][w_list[ii].Length - 1].ToString() == "B")
{
w = 11;
}
else if (w_list[ii][w_list[ii].Length - 1].ToString() == "C")
{
w = 12;
}
else if (w_list[ii][w_list[ii].Length - 1].ToString() == "D")
{
w = 13;
}
else if (w_list[ii][w_list[ii].Length - 1].ToString() == "E")
{
w = 14;
}
else if (w_list[ii][w_list[ii].Length - 1].ToString() == "F")
{
w = 15;
}
else
{
w = int.Parse(w_list[ii][w_list[ii].Length - 1].ToString());
}
string temp = "";
for (int iii = 0; iii < w_list[ii].Length - 1; iii++)
{
temp += w_list[ii][iii].ToString();
}
getedData += Convert.ToChar((int.Parse(temp) * 16) + w).ToString();
}
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(getedData);
break;
}
}
}
else if (choice == "3")
{
Console.WriteLine();
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("1.Add User");
Console.WriteLine("2.Delele User");
Console.WriteLine("3.change UserPermissions");
Console.WriteLine("4.Change Password");
ConsoleKeyInfo ki = new ConsoleKeyInfo();
ki= Console.ReadKey();
if (ki.KeyChar.ToString() == "1")
{
Console.Clear();
Console.Write("enter new UserName:");
string Newuser=Console.ReadLine();
Console.Write("enter new Pssword:");
string newPassword = "";
while (true)
{
ConsoleKeyInfo kii = new ConsoleKeyInfo();
kii = Console.ReadKey();
if (kii.Key == ConsoleKey.Enter)
{
break;
}
newPassword += kii.KeyChar.ToString();
Console.Write(Convert.ToChar(8));
Console.Write("*");
}
Console.WriteLine();
Console.Write("permissions for:");
string permission = Console.ReadLine();
Console.WriteLine();
Console.Write("userType(Admin/user):");
string usertype= Console.ReadLine();
try
{
OleDbCommand comIn = new OleDbCommand("insert into t1 values('" + Newuser + "','" + newPassword + "','" + permission + "','All','" + usertype + "')", con);
con.Open();
comIn.ExecuteNonQuery();
con.Close();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(" <<<user has been Added>>>");
Console.WriteLine();
}
catch (Exception ei)
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(" <<<"+ei.Message+">>>");
Console.WriteLine();
}
}
else if (ki.KeyChar.ToString() == "2")
{
Console.Clear();
Console.WriteLine();
Console.Write("Enter UserName To Delete:");
string user = Console.ReadLine();
try
{
OleDbCommand comDel = new OleDbCommand("delete from t1 where username='" + user + "'", con);
con.Open();
comDel.ExecuteNonQuery();
con.Close();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(" <<<user has been Deleted>>>");
Console.WriteLine();
}
catch (Exception ei)
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(" <<<" + ei.Message + ">>>");
Console.WriteLine();
}
}
else if (ki.KeyChar.ToString() == "3")
{
Console.Clear();
Console.WriteLine();
Console.Write("Enter UserName:");
string user = Console.ReadLine();
Console.WriteLine();
Console.Write("permissions for:");
string permission = Console.ReadLine();
Console.WriteLine();
Console.Write("userType(Admin/user):");
string usertype = Console.ReadLine();
try
{
OleDbCommand comUp = new OleDbCommand("update t1 set permissions='" + permission + "',UserType='" + usertype + "' where username='" + user + "'", con);
con.Open();
comUp.ExecuteNonQuery();
con.Close();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(" <<<user detail has been changed>>>");
Console.WriteLine();
}
catch (Exception ei)
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(" <<<" + ei.Message + ">>>");
Console.WriteLine();
}
}
else if (ki.KeyChar.ToString() == "4")
{
Console.Clear();
Console.Write("enter your Current Password:");
string currentPassword = "";
while (true)
{
ConsoleKeyInfo kii = new ConsoleKeyInfo();
kii = Console.ReadKey();
if (kii.Key == ConsoleKey.Enter)
{
break;
}
currentPassword += kii.KeyChar.ToString();
Console.Write(Convert.ToChar(8));
Console.Write("*");
}
Console.WriteLine();
Console.Write("enter your new Password:");
string newPassword = "";
while (true)
{
ConsoleKeyInfo kii = new ConsoleKeyInfo();
kii = Console.ReadKey();
if (kii.Key == ConsoleKey.Enter)
{
break;
}
newPassword += kii.KeyChar.ToString();
Console.Write(Convert.ToChar(8));
Console.Write("*");
}
Console.WriteLine();
Console.Write("enter your new Password:");
string confPassword = "";
while (true)
{
ConsoleKeyInfo kii = new ConsoleKeyInfo();
kii = Console.ReadKey();
if (kii.Key == ConsoleKey.Enter)
{
break;
}
confPassword += kii.KeyChar.ToString();
Console.Write(Convert.ToChar(8));
Console.Write("*");
}
if (newPassword == confPassword)
{
if (currentPassword == p)
{
try
{
OleDbCommand commUp = new OleDbCommand("update t1 set password='" + newPassword + "' where username='" + userName + "'", con);
con.Open();
commUp.ExecuteNonQuery();
con.Close();
Console.WriteLine("Password changed");
}
catch (Exception ei)
{
Console.WriteLine(ei.Message);
}
}
else
{
Console.WriteLine("Invalid Password");
}
}
else
{
Console.WriteLine("password misMatch");
}
}
}
else if (choice == "4")
{
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Closing application...");
Thread.Sleep(1000);
Thread.CurrentThread.Abort();
}
Console.ReadLine();
Console.Clear();
Console.ResetColor();
goto top;
}
//if userlogedIn
else if (dt.Rows[0][4].ToString() == "user")
{
top:
Console.ResetColor();
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.Title = "<<<< Welcome To secure world >>>> User";
Console.WriteLine("**** Select a option ****");
Console.WriteLine("1.Read Logs");
Console.WriteLine("2.Settings");
Console.WriteLine("3.Exit");
Console.Write("enter your choice:");
string choice;
try
{
choice = Console.ReadLine();
if (!(choice == "1" || choice == "2"|| choice == "3"))
{
Console.Clear();
Console.WriteLine("*** invalid f choice ***");
goto top;
}
}
catch
{
Console.Clear();
Console.WriteLine("*** invalid choice ***");
goto top;
}
if (choice == "1")
{
Console.ResetColor();
Console.Clear();
Console.ForegroundColor = ConsoleColor.DarkRed;
string str = dt.Rows[0][2].ToString();
string[] EntryTypeList = Directory.GetDirectories(AppDomain.CurrentDomain.BaseDirectory + "\\" + Environment.MachineName);
Console.WriteLine("**** Select logType ****");
for (int i = 0; i < EntryTypeList.Length; i++)
{
DirectoryInfo di = new DirectoryInfo(EntryTypeList[i]);
if (str.IndexOf(di.Name) != -1)
{
Console.WriteLine(i + 1 + "." + di.Name);
}
}
Console.Write("enter your choice:");
choice = Console.ReadLine();
Console.Clear();
Console.WriteLine("**** Select LogProvider ****");
string[] logen = Directory.GetDirectories(EntryTypeList[int.Parse(choice) - 1]);
for (int i = 0; i < logen.Length; i++)
{
DirectoryInfo di = new DirectoryInfo(logen[i]);
Console.WriteLine(i + 1 + "." + di.Name);
}
Console.Write("enter your choice:");
choice = Console.ReadLine();
Console.Clear();
Console.WriteLine("**** Select LogProvider ****");
string[] logSource = Directory.GetDirectories(logen[int.Parse(choice) - 1]);
for (int i = 0; i < logSource.Length; i++)
{
DirectoryInfo di = new DirectoryInfo(logSource[i]);
Console.WriteLine(i + 1 + "." + di.Name);
}
Console.Write("enter your choice:");
choice = Console.ReadLine();
Console.Clear();
Console.WriteLine("**** Select Log-Index ****");
Console.WriteLine("index\t\tDate");
string[] logDate = Directory.GetDirectories(logSource[int.Parse(choice) - 1]);
Console.ForegroundColor = ConsoleColor.Red;
for (int i = 0; i < logDate.Length; i++)
{
DirectoryInfo di = new DirectoryInfo(logDate[i]);
string[] logIndex = Directory.GetFiles(logDate[i]);
for (int j = 0; j < logIndex.Length; j++)
{
FileInfo fi = new FileInfo(logIndex[j]);
Console.WriteLine(fi.Name.Remove(fi.Name.IndexOf(".log")) + "\t" + di.Name);
}
}
Console.Write("enter Index-Number:");
string fileName = Console.ReadLine();
for (int i = 0; i < logDate.Length; i++)
{
if (File.Exists(logDate[i] + "\\" + fileName + ".log"))
{
string FileData = File.ReadAllText(logDate[i] + "\\" + fileName + ".log");
string getedData = "";
int count = 0;
for (int r = 0; r < FileData.Length; r++)
{
if (FileData[r].ToString() == " ")
{
count++;
}
}
string[] w_list = new string[count];
int w_index = 0;
for (int r = 0; r < FileData.Length; r++)
{
if (FileData[r].ToString() == " ")
{
w_index++;
}
else
{
w_list[w_index] += FileData[r].ToString(); ;
}
}
for (int ii = 0; ii < w_list.Length; ii++)
{
int w = 0;
if (w_list[ii][w_list[ii].Length - 1].ToString() == "A")
{
w = 10;
}
else if (w_list[ii][w_list[ii].Length - 1].ToString() == "B")
{
w = 11;
}
else if (w_list[ii][w_list[ii].Length - 1].ToString() == "C")
{
w = 12;
}
else if (w_list[ii][w_list[ii].Length - 1].ToString() == "D")
{
w = 13;
}
else if (w_list[ii][w_list[ii].Length - 1].ToString() == "E")
{
w = 14;
}
else if (w_list[ii][w_list[ii].Length - 1].ToString() == "F")
{
w = 15;
}
else
{
w = int.Parse(w_list[ii][w_list[ii].Length - 1].ToString());
}
string temp = "";
for (int iii = 0; iii < w_list[ii].Length - 1; iii++)
{
temp += w_list[ii][iii].ToString();
}
getedData += Convert.ToChar((int.Parse(temp) * 16) + w).ToString();
}
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(getedData);
break;
}
}
}
else if (choice == "2")
{
Console.WriteLine("1.Change Password");
ConsoleKeyInfo ki = new ConsoleKeyInfo();
ki= Console.ReadKey();
if (ki.KeyChar.ToString() == "1")
{
Console.Clear();
Console.Write("enter your Current Password:");
string currentPassword = "";
while (true)
{
ConsoleKeyInfo kii = new ConsoleKeyInfo();
kii = Console.ReadKey();
if (kii.Key == ConsoleKey.Enter)
{
break;
}
currentPassword += kii.KeyChar.ToString();
Console.Write(Convert.ToChar(8));
Console.Write("*");
}
Console.WriteLine();
Console.Write("enter your new Password:");
string newPassword = "";
while (true)
{
ConsoleKeyInfo kii = new ConsoleKeyInfo();
kii = Console.ReadKey();
if (kii.Key == ConsoleKey.Enter)
{
break;
}
newPassword += kii.KeyChar.ToString();
Console.Write(Convert.ToChar(8));
Console.Write("*");
}
Console.WriteLine();
Console.Write("enter your new Password:");
string confPassword = "";
while (true)
{
ConsoleKeyInfo kii = new ConsoleKeyInfo();
kii = Console.ReadKey();
if (kii.Key == ConsoleKey.Enter)
{
break;
}
confPassword += kii.KeyChar.ToString();
Console.Write(Convert.ToChar(8));
Console.Write("*");
}
if (newPassword == confPassword)
{
if (currentPassword == p)
{
try
{
OleDbCommand commUp = new OleDbCommand("update t1 set password='" + newPassword + "' where username='" + userName + "'", con);
con.Open();
commUp.ExecuteNonQuery();
con.Close();
Console.WriteLine("Password changed");
}
catch (Exception ei)
{
Console.WriteLine(ei.Message);
}
}
else
{
Console.WriteLine("Invalid Password");
}
}
else
{
Console.WriteLine("password misMatch");
}
}
}
else if (choice == "3")
{
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Closing application...");
Thread.Sleep(1000);
Thread.CurrentThread.Abort();
}
Console.ReadLine();
Console.Clear();
Console.ResetColor();
goto top;
}
}
}
}
project screenshot
output screen
by:Pankaj Sharma