zoukankan      html  css  js  c++  java
  • 数据库还原状态监控(查看事件查看器)

    由于备份服务器每天都还原生产服务器传来的新的数据,如何查看数据库还原状态是成功还是失败?

    我们将通过查看“控制面板”—〉“管理工具”—〉“事件查看器”来查看还原情况。

    下面我们将设置一个邮件提醒功能,监控每天凌晨数据库运行的状态,将还原成功和错误信息都发送到邮件中。

    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("数据库已还原"))
                       {
                            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  邮件 Client设置
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            client.Host = "mail.126.com";
            client.UseDefaultCredentials = 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 = "数据库还原状态";                  //邮件主题
            message.Body = emailmessage;                         //邮件内容
            message.To.Add("name@126.com");                      //收件人
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            message.Priority = System.Net.Mail.MailPriority.High;//邮件优先级

            client.Send(message); 

      }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

  • 相关阅读:
    多测师讲解html _伪类选择器17_高级讲师肖sir
    多测师讲解html _后代选择器16_高级讲师肖sir
    多测师讲解html _组合选择器_高级讲师肖sir
    多测师讲解html _标签选择器14_高级讲师肖sir
    前端 HTML form表单标签 input标签 type属性 重置按钮 reset
    前端 HTML form表单标签 textarea标签 多行文本
    前端 HTML form表单标签 input标签 type属性 file 上传文件
    前端 HTML form表单标签 input标签 type属性 radio 单选框
    前端 HTML form表单标签 input标签 type属性 checkbox 多选框
    前端 HTML form表单目录
  • 原文地址:https://www.cnblogs.com/Anlycp/p/1501220.html
Copyright © 2011-2022 走看看