Database restoration status monitoring (view Event Viewer)

Source: Internet
Author: User
Tags mailmessage smtpclient

Since the backup server restores new data from the production server every day, how can I check whether the database restore status is successful or failed?

We will view the restoration by viewing "Control Panel"> "Administrative Tools"> "Event Viewer.

Next we will set up an email reminder function to monitor the database running status in the early morning of every day, and send the restored success and error messages to the mail.

 

Static void main (string [] ARGs)
{
Sendmailaboutcheckserverstatus ();
Console. readkey ();
}

Private Static void sendmailaboutcheckserverstatus ()
{
EventLog elog = new EventLog ();
Elog. log = "application ";
String emailmessage = "";
For (INT I = 0; I <elog. Entries. Count; I ++)
{
Try
{
If (elog. Entries [I]. Source = "MSSQLServer ")
{
Datetime starttime = convert. todatetime (datetime. Now. tostring ("yyyy-mm-dd") + "00:00:00 ");
Datetime endtime = convert. todatetime (datetime. Now. tostring ("yyyy-mm-dd") + "8:30:00 ");
If (elog. Entries [I]. timegenerated> starttime & elog. Entries [I]. timegenerated <endtime)
{
If (elog. Entries [I]. entrytype. tostring () = "error ")
{
Emailmessage + = "message:" + elog. Entries [I]. Message + "<br>" +
"App:" + elog. Entries [I]. Source + "<br>" +
"Entry type:" + elog. Entries [I]. entrytype + "<br>" +
"Timegenerated:" + elog. Entries [I]. timegenerated + "<br> ";
}
If (elog. Entries [I]. Message. Contains ("database restored "))
{
Emailmessage + = "message:" + elog. Entries [I]. Message + "<br>" +
"App:" + elog. Entries [I]. Source + "<br>" +
"Entry type:" + elog. Entries [I]. entrytype + "<br>" +
"Timegenerated:" + elog. Entries [I]. timegenerated + "<br> ";
}
}
}
}
Catch {}
}
Sendemailinfo (emailmessage );
Console. writeline ("end! ");
}

 

Private Static void sendemailinfo (string emailmessage)
{

Try
{
# Region email client settings
System. net. Mail. smtpclient client = new system. net. Mail. smtpclient ();
Client. Host = "mail.126.com ";
Client. usedefacrecredentials = false;
Client. Credentials = new system. net. networkcredential ("namn@126.com", "password ");
Client. deliverymethod = system. net. Mail. smtpdeliverymethod. Network;

System. net. Mail. mailmessage message = new system. net. Mail. mailmessage ();
Message. From = new system. net. Mail. mailaddress ("name@126.com", "displayname", system. Text. encoding. utf8 );
# Endregion

Message. Subject = "database restoration status"; // Email Subject
Message. Body = emailmessage; // mail content
Message. to. Add ("name@126.com"); // recipient
Message. bodyencoding = system. Text. encoding. utf8;
Message. isbodyhtml = true;
Message. Priority = system. net. Mail. mailpriority. High; // mail priority

Client. Send (Message );

}
Catch (exception ex)
{
Console. writeline (ex. Message );
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.