📄 customcounterinstances.aspx
字号:
<%@ Page debug="true" Trace="false" %>
<%@ Import Namespace="System.Diagnostics" %>
<html>
<head>
<!--------------------------------------------------------------------------->
<html>
<script language="C#" runat="server">
String Category = "Wrox";
String Counter = "Web Services";
void page_load(Object Sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (PerformanceCounterCategory.Exists(Category))
AlreadyExists.Visible = true;
}
}
void CreateCounters_Click(Object Sender, EventArgs e)
{
// only create the counters if they don't already exist
if (!PerformanceCounterCategory.Exists(Category))
{
// set up the counter details
CounterCreationDataCollection ccdc = new CounterCreationDataCollection();
ccdc.Add(new CounterCreationData(Counter, "Web Service Requests", PerformanceCounterType.NumberOfItems32));
PerformanceCounterCategory.Create(Category, "Wrox Press Professional ASP.NET Samples", ccdc);
}
ShowCounters();
}
void ProcessCounter_Click(Object Sender, CommandEventArgs e)
{
if (PerformanceCounterCategory.Exists(Category))
{
// CommandArgument contains the name of the counter
PerformanceCounter pc = new PerformanceCounter(Category, Counter, e.CommandArgument.ToString(), false);
pc.Increment();
pc.InstanceName = "_Total";
pc.Increment();
ShowCounters();
}
}
void DeleteCounters_Click(Object Sender, EventArgs e)
{
// delete all counters and the catageory
if (PerformanceCounterCategory.Exists(Category))
{
PerformanceCounterCategory.Delete(Category);
Page.Controls.Remove(CounterValues);
}
}
// create a table showing the counter values
void ShowCounters()
{
if (PerformanceCounterCategory.Exists(Category))
{
PerformanceCounterCategory cat = new PerformanceCounterCategory(Category);
String[] instNames;
// get the instance names
instNames = cat.GetInstanceNames();
// if more than one isntance we need to loop through the instances
if (instNames.Length == 0)
ShowCounterValues(cat.GetCounters());
else
for (int i = 0; i < instNames.Length - 1; i++)
ShowCounterValues(cat.GetCounters(instNames[i]));
}
}
// display the data for a collection of counters
void ShowCounterValues(PerformanceCounter[] pca)
{
String s;
TableRow tr;
TableCell tcc;
TableCell tcv;
TableCell tct;
HtmlImage img;
// now loop through the counters
foreach (PerformanceCounter pc in pca)
{
// cell for text
tcc = new TableCell();
tcc.Text = pc.CounterHelp + ": " + pc.InstanceName;
tcc.Width = Unit.Percentage(30);
// text value
tct = new TableCell();
tct.Text = pc.RawValue.ToString();
tct.Width = Unit.Percentage(5);
// bar graph and cell containing it
img = new HtmlImage();
img.Src = "RedPixel.gif";
img.Width = (int)pc.RawValue;
img.Height = 20;
tcv = new TableCell();
tcv.Controls.Add(img);
// add cells to row, and rows to table
tr = new TableRow();
tr.Cells.Add(tcc);
tr.Cells.Add(tct);
tr.Cells.Add(tcv);
CounterValues.Rows.Add(tr);
}
}
</script>
<form runat="server">
<asp:Button Text="Create Counters" onclick="CreateCounters_Click" runat="server"/>
<asp:Button Text="Delete Counters" onclick="DeleteCounters_Click" runat="server"/>
<p/>
<table cellspacing="10">
<tr>
<td align="center">Instance 1</td>
<td align="center">Instance 2</td>
<td align="center">Instance 3</td>
</tr>
<tr>
<td>
<asp:Button CommandArgument="Instance1" Text="Increment"
onCommand="ProcessCounter_Click" runat="server"/>
</td>
<td>
<asp:Button CommandArgument="Instance2" Text="Increment"
onCommand="ProcessCounter_Click" runat="server"/>
</td>
<td>
<asp:Button CommandArgument="Instance3" Text="Increment"
onCommand="ProcessCounter_Click" runat="server"/>
</td>
</tr>
</table>
<p/>
<asp:Table id="CounterValues" width="75%" runat="server">
<asp:TableRow>
<asp:TableHeaderCell HorizontalAlign="Left">Counter</asp:TableHeaderCell>
<asp:TableHeaderCell HorizontalAlign="Left">Value</asp:TableHeaderCell>
<asp:TableHeaderCell HorizontalAlign="Left">Value</asp:TableHeaderCell>
</asp:TableRow>
</asp:table>
<asp:Label id="AlreadyExists"
Text="The category 'Wrox' already exists. You should delete it before continuing."
visible = "false" runat="server"/>
</form>
<!--------------------------------------------------------------------------->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -