Hi Folks,
Have a requirement as follows:
1. How to write log in event viewer in C#
2.After getting the event log need to pass the message through an email using C#.
3.How to get the list of jobs and status running on a remote server using C#.
Any suggestion on this please.
Thanks,
Sisir
Sisir BeheraPosted May 16, 2013, 4:17 AM
Thanks for your reply.I did tried the same code you suggested.Have got lots of errors.Not sure where n how i need to implement your suggestion to fulfill my requirement.my sample code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32.TaskScheduler;
using System.Threading;
using System.Diagnostics;
using System.Net;
using System.Net.Mail;
//using System.Diagonistics.EventLog;
namespace
{
{
{
{
{
Task taskdDef = task.FindTask(
TaskSchedulerRemoteclass Programstatic void Main(string[] args)using (TaskService task = new TaskService())while (true)"RF Report");//string registeredTask;
//string[] taskCollection = { "", "", "" };
//foreach (string str in taskCollection)
//{
//For Each registeredTask In taskCollection
{
Console.WriteLine(
Console.WriteLine(
Console.WriteLine(
Console.WriteLine(
Console.WriteLine(
}
Thread.Sleep(10000);
MailMessage Msg =
MailAddress fromMail =
switch (taskdDef.State)case TaskState.Queued:"Queued");break;case TaskState.Ready:"Ready");break;case TaskState.Unknown:"Unknown");break;case TaskState.Disabled:"Disabled");break;case TaskState.Running:"Running");break;new MailMessage();new MailAddress([email protected]);// Sender e-mail address.Msg.From = fromMail;
// Recipient e-mail address.Msg.To.Add(
Msg.Priority =
new MailAddress([email protected]));MailPriority.High;// Subject of e-mailMsg.Subject =
Msg.Body +=
Msg.Body +=
Msg.IsBodyHtml =
sSmtpServer =
a.Host = sSmtpServer;
a.EnableSsl =
"Scheduled Task Status Report:" + System.DateTime.Now;"
a.Send(Msg);
}
{
Console.WriteLine(
Console.WriteLine(ep.Message);
}
Console.WriteLine("Sending mail...");Console.WriteLine("Mail was sent successfully!");catch (Exception ep)"failed to send mail:");//}}
EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Error, 234);
}
}
}
}
string sSource = "MyCustom Group";string sLog = "Application";string sEvent = "";if (!EventLog.SourceExists(sSource))
Please suggest.
Abhishek SurPosted May 15, 2013, 12:29 AM
1. Writing an EventLog in windows environment using .NEt is very easy. Use System.Diagonistics.EventLog.
This is a sample code. Source is the group of Events, it is used to identify the Log entry. There is also EventLogEntryType which indicates the alert icon for the EventLog entry and also a custom eventlog number which helps you to identify the error number.
2. Mailing is also not a big deal. There are lot of articles available on the internet which can guide you to send mails. To Send mails from .NET environment you need to use SmtpClient. I have a post on sending mail using Gmail Server :
http://www.codeproject.com/Articles/20546/How-to-Send-Mails-from-your-GMAIL-Account-through
This can help you to get through this.
3. Hmm, You can use ManagementObject to do that. It automatically handles impersonation for you so it is easier to implement. Here is some code for you.
I hope this helps.