📄 customcounters.aspx
字号:
<%@ Page debug="true" Trace="false" %>
<%@ Import Namespace="System.Diagnostics" %>
<html>
<head>
<title>Custom Performance Counters</title>
</head>
<body>
<!--------------------------------------------------------------------------->
<script language="C#" runat="server">
void Page_Load(Object Sender, EventArgs E)
{
if (Page.IsPostBack)
{
// if the category already exist, increment the postback counter
if (PerformanceCounterCategory.Exists("Wrox"))
IncrementCounter("Wrox", "PostBack");
}
}
void IncrementCounter(String Category, String Counter)
{
if (PerformanceCounterCategory.Exists("Wrox"))
{
PerformanceCounter pc = new PerformanceCounter(Category, Counter, false);
pc.Increment();
}
}
void DecrementCounter(String Category, String Counter)
{
if (PerformanceCounterCategory.Exists("Wrox"))
{
PerformanceCounter pc = new PerformanceCounter(Category, Counter, false);
if (pc.RawValue > 0)
pc.Decrement();
}
}
void CreateCounters_Click(Object Sender, EventArgs E)
{
// only create the counters if they don't already exist
if (!PerformanceCounterCategory.Exists("Wrox"))
{
// set up the counter details
CounterCreationDataCollection ccdc = new CounterCreationDataCollection();
ccdc.Add(new CounterCreationData("Counter1", "Counter 1", PerformanceCounterType.NumberOfItems32));
ccdc.Add(new CounterCreationData("Counter2", "Counter 2", PerformanceCounterType.NumberOfItems32));
ccdc.Add(new CounterCreationData("Counter3", "Counter 3", PerformanceCounterType.NumberOfItems32));
ccdc.Add(new CounterCreationData("PostBack", "Postback occurred", PerformanceCounterType.NumberOfItems32));
PerformanceCounterCategory.Create("Wrox", "Wrox Press Professional ASP.NET Samples", ccdc);
}
ShowCounterValues();
}
void ProcessCounter_Click(Object Sender, CommandEventArgs e)
{
if (PerformanceCounterCategory.Exists("Wrox"))
{
// The CommandName contains what we want to do (Increment/Decrement), and
// the CommandArgument contains the name of the counter
if (e.CommandName == "Increment")
IncrementCounter("Wrox", e.CommandArgument.ToString());
else
DecrementCounter("Wrox", e.CommandArgument.ToString());
ShowCounterValues();
}
}
void DeleteCounters_Click(Object Sender, EventArgs E)
{
// delete all counters and the catageory
if (PerformanceCounterCategory.Exists("Wrox"))
{
PerformanceCounterCategory.Delete("Wrox");
Page.Controls.Remove(CounterValues);
}
}
// create a table showing the counter values
void ShowCounterValues()
{
if (PerformanceCounterCategory.Exists("Wrox"))
{
PerformanceCounterCategory cat = new PerformanceCounterCategory("Wrox");
String s;
TableRow tr;
TableCell tcc;
TableCell tcv;
TableCell tct;
HtmlImage img;
// Construct a table row for each counter
foreach (PerformanceCounter pc in cat.GetCounters())
{
// cell for text
tcc = new TableCell();
tcc.Text = pc.CounterHelp;
tcc.Width = Unit.Percentage(20);
// 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">Counter 1</td>
<td align="center">Counter 2</td>
<td align="center">Counter 3</td>
</tr>
<tr>
<td>
<asp:Button CommandName="Increment" CommandArgument="Counter1" Text="Increment"
onCommand="ProcessCounter_Click" runat="server"/>
<asp:Button CommandName="Decrement" CommandArgument="Counter1" Text="Decrement"
onCommand="ProcessCounter_Click" runat="server"/>
</td>
<td>
<asp:Button CommandName="Increment" CommandArgument="Counter2" Text="Increment"
onCommand="ProcessCounter_Click" runat="server"/>
<asp:Button CommandName="Decrement" CommandArgument="Counter2" Text="Decrement"
onCommand="ProcessCounter_Click" runat="server"/>
</td>
<td>
<asp:Button CommandName="Increment" CommandArgument="Counter3" Text="Increment"
onCommand="ProcessCounter_Click" runat="server"/>
<asp:Button CommandName="Decrement" CommandArgument="Counter4" Text="Decrement"
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>
</form>
<!--------------------------------------------------------------------------->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -