somebody can modify it for me ?thanks!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i want Console write the EventLog message when the source = "Print", code as
following :

using System;
using System.Data;
using System.Diagnostics;
using System.Text.RegularExpressions;
public class LogTest
{
public static void Main(String[] args)
{
string machine = ".";
string log = "system";
EventLog aLog = new EventLog();
aLog.Log = log;
aLog.MachineName = machine;
int Num = aLog.Entries.Count;
Console.WriteLine("total"+Num.ToString());
while(aLog.Source.ToString().Equals("Print"))


{ foreach (EventLogEntry entry in aLog.Entries)
{
Console.WriteLine("\t项:{0}", entry.Message);
}
}

}
}
 
You'll probably need this code:

//Using
using System.Diagnostics;
using System.Text;

//Method Body
EventLog log = new EventLog("System",".");
StringBuilder sb = new StringBuilder();
int count = 0;
foreach(EventLogEntry entry in log.Entries)
{
if(entry.Source == "Print")
{
sb = sb.Append(entry.Message).Append(Environment.NewLine);
count++;
}
}

Console.WriteLine("Total No. of Entries: " + count);
Console.WriteLine(sb);

HTH, Cheers :)

Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
Hi,Maqsood Ahmed, Thanks for your kindly help, and more questions i want to
ask you, it seems it 's painful to get the remote host print message with
your code, also for mutil-host, do you have any solution for remote&mutil
host ?
 
Hello,
I didn't experience any problem with Event Logs from remote system.
Can you paste the code you were trying for getting logs from remote
system?
Bye

Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
Maqsood, now i can get the Message for EventLog, but failed write the Message
string to a MDB, Would you please check my code, as below:


//-----------------------------------------------------------------------
//Wrote by Michael April 30 2005
//-----------------------------------------------------------------------

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

public class LogInfo4
{
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);
//get string for print Message
foreach (EventLogEntry entry in aLog.Entries)
{

string strText = entry.Message;
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();
OleDbConnection conn = new OleDbConnection();
// TODO: Modify the connection string and include any
// additional required properties for database.
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= E:\April30\Db\TestDb.mdb" ;


conn.Open();
string sqlInsert=@"insert into
TestTable(DocNum,DocName,User,ServerName,Byte,Ip,Pages) values
(@PrintDocNum,@PrintDocName,@PrintUser,@PrintServerName,@PrintByte,@PrintIp,@PrintPages)";
OleDbCommand cmd1=new OleDbCommand(sqlInsert,conn);
cmd1.Parameters.Add("@PrintDocNum",System.Data.OleDb.OleDbType.VarChar,50,PrintDocNum);

cmd1.Parameters.Add("@PrintDocName",System.Data.OleDb.OleDbType.VarChar,50,PrintDocName);
cmd1.Parameters.Add("@PrintUser",System.Data.OleDb.OleDbType.VarChar,50,PrintUser);
cmd1.Parameters.Add("@PrintServerName",System.Data.OleDb.OleDbType.VarChar,50,PrintServerName);
cmd1.Parameters.Add("@PrintByte",System.Data.OleDb.OleDbType.VarChar,50,PrintByte);
cmd1.Parameters.Add("@PrintIp",System.Data.OleDb.OleDbType.VarChar,50,PrintIp);
cmd1.Parameters.Add("@PrintPages",System.Data.OleDb.OleDbType.VarChar,50,PrintPages);
//Console.WriteLine("Command Created");
cmd1.ExecuteNonQuery();//insert string
//Console.WriteLine("insert end");
conn.Close();
}

}
}
}






Maqsood Ahmed said:
Hello,
I didn't experience any problem with Event Logs from remote system.
Can you paste the code you were trying for getting logs from remote
system?
Bye

Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
Back
Top