JinGanTec Studio@桂花园

大量多年原生桂花树待售中;Coding in Csharp,Ruby,VBS!

导航

Eventlog 的应用,Monitor Printers from EventLog

//--------------------------------------------------------------------------
//Wrote by Roopeman June 19 2005,the program just for update database ,
//another block code is for write whole eventlog to database.
//--------------------------------------------------------------------------

using System.Text;
using System.Diagnostics;
using System.Threading;
using System;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

public class UpdateLog
{
public static void Main(String[] args)
{

string log="System";
string machine="MyDc001";
EventLog aLog = new EventLog();
aLog.Log = log;
aLog.MachineName = machine;

Console.WriteLine("There are {0} entr[y|ies] in the log:",
aLog.Entries.Count);

SqlConnection conn=new SqlConnection("Initial Catalog=TestDB2;Data Source=(local);Integrated Security=SSPI;");
//get the last datetime from table
string DbTime= "select Max(LogDate) from LogTable";
SqlCommand cmd = new SqlCommand(DbTime ,conn);
conn.Open();
string TT = cmd.ExecuteScalar().ToString();

conn.Close();

for(int ee=aLog.Entries.Count-1;ee>0;ee--)
 {
//get EventLog time
string LogTime = aLog.Entries[ee].TimeWritten.ToString();

if (Convert.ToDateTime(LogTime)>Convert.ToDateTime(TT))
 {

string strText = aLog.Entries[ee].Message;
string strDate = aLog.Entries[ee].TimeWritten.ToString();

string PrintDocNum, PrintDocName,PrintUser,PrintServerName,PrintByte,PrintIp,PrintPages;
   string strMatch = @"^Document (\d+), *(.+) owned by (.+) was printed on (.*) via port IP_(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\.  Size in bytes: (\d+); pages printed: (\d+)";
   Regex re = new Regex(strMatch);
   Match m = re.Match(strText);
   if(m.Success)
        {
    PrintDocNum = m.Groups[1].ToString();
    PrintDocName  = m.Groups[2].ToString();
    PrintUser     = m.Groups[3].ToString();
    PrintServerName = m.Groups[4].ToString();
    PrintIp       = m.Groups[5].ToString();
    PrintByte = m.Groups[6].ToString();
    PrintPages     = m.Groups[7].ToString();

 

string sqlInsert=@"insert into LogTable(DocNum,DocName,DocUser,DocServerName,DocIP,DocByte,DocPages,LogDate) Values (@DocNum,@DocName,@DocUser,@DocServerName,@DocIp,@DocByte,@DocPages,@LogDate)";
SqlCommand cmd1=new SqlCommand(sqlInsert,conn);
cmd1.Parameters.Add("@DocNum",SqlDbType.NVarChar,50).Value = PrintDocNum;
cmd1.Parameters.Add("@DocName",SqlDbType.NVarChar,100).Value = PrintDocName;
cmd1.Parameters.Add("@DocUser",SqlDbType.NVarChar,50).Value = PrintUser;
cmd1.Parameters.Add("@DocServerName",SqlDbType.NVarChar,50).Value = PrintServerName;
cmd1.Parameters.Add("@DocByte",SqlDbType.NVarChar,50).Value = PrintByte;
cmd1.Parameters.Add("@DocIp",SqlDbType.NVarChar,50).Value = PrintIp;
cmd1.Parameters.Add("@DocPages",SqlDbType.NVarChar,50).Value = PrintPages;
cmd1.Parameters.Add("@LogDate",SqlDbType.NVarChar,50).Value = strDate;


try
{
    conn.Open();
    cmd1.ExecuteNonQuery();
}
catch(Exception ex)
{
    Console.WriteLine(ex.Message.ToString());
}
finally
{
    conn.Close();
}

}
}
}
}
}

posted on 2005-06-21 11:33  Roopeman  阅读(229)  评论(0编辑  收藏  举报