domingo, 11 de agosto de 2013

Monitorar o Desempenho do Servidor com C#

Utilizando o ASP.NET com C# para monitorar o desempenho do Windows, neste exemplo vamos ver o desempenho e processos.






Nossa Default.aspx:


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head runat="server">
    <title>Edgar Esteves Status WebServ</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Status do WebServ</h1>
<i>Estatísticas geradas a partir de: <%=DateTime.Now.ToLongDateString() %> at <%=DateTime.Now.ToLongTimeString()%>....</i>
<br />
<br />
<b>Disponível:</b><asp:Label ID="Label1" runat="server" Width="251px"></asp:Label>
<br />
<b>
<br />
Total de Processos:</b>
<asp:Label ID="Label2" runat="server" Width="247px"></asp:Label>
<br />
<br />
<b>Total de Threding:</b><asp:Label ID="Label3" runat="server" Width="254px"></asp:Label>
      <br />
<br />
<br />
<asp:ListBox ID="ListBox1" runat="server" Height="357px" Width="368px"></asp:ListBox><br />
<br />
<br />
<br />
    </div>
    </form>
</body>
</html>


Nossa Default.aspx.cs:

Lembre-se de Referenciar:
using System.Net;
using System.Diagnostics;

Veja ela completa:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Diagnostics;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PerformanceCounter objMemperf = new PerformanceCounter("Memory", "Available Bytes");
        PerformanceCounter objProcperf = new PerformanceCounter("System", "Processes");
        PerformanceCounter objComperf = new PerformanceCounter("System", "Threads");

        Label1.Text = string.Format("{0:#,###}", objMemperf.NextValue()) + "Byte";
        Label2.Text = objProcperf.NextValue().ToString();
        Label3.Text = objComperf.NextValue().ToString();

        if (!Page.IsPostBack)
        {
            foreach (PerformanceCounterCategory objPer in PerformanceCounterCategory.GetCategories())
            {
                ListBox1.Items.Add(new ListItem(objPer.CategoryName));
            }
        }
    }
}

0 comentários:

Postar um comentário