通过ASP.Net确定Windows Server的运行时
发布时间:2020-12-16 06:42:33 所属栏目:asp.Net 来源:网络整理
导读:是否在任何ASP.net类中都有一个内置属性,用于确定运行Web应用程序的Web服务器的正常运行时间是什么? 突然出现在我脑海中的一些可能性: global.asax和Application_OnStart事件.获取时间戳并确定正常运行时间,我只是从DateTime.Now中减去它 编写一个Windows
是否在任何ASP.net类中都有一个内置属性,用于确定运行Web应用程序的Web服务器的正常运行时间是什么?
突然出现在我脑海中的一些可能性: > global.asax和Application_OnStart事件.获取时间戳并确定正常运行时间,我只是从DateTime.Now中减去它 但这两者都没有提供“内置”解决方案.我可能会在.net框架中监督一些事情吗? 解决方法
要获得系统正常运行时间,请使用以下代码:
TimeSpan uptime; using (var uptimeCounter = new PerformanceCounter("System","System Up Time")) { uptimeCounter.NextValue(); uptime = TimeSpan.FromSeconds(uptimeCounter.NextValue()); } 编辑:请注意,部分受信任的代码不能使用它. 我在ASP.Net中写了一个服务器状态页面,显示服务器正常运行时间等. <%@ Page Title="Server Stats" Language="C#" MasterPageFile="~/Admin.Master" AutoEventWireup="true" CodeFile="Stats.aspx.cs" Inherits="Stats" %> <%@ Import Namespace="System.Diagnostics" %> <%@ Import Namespace="Microsoft.VisualBasic.Devices" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> <style type="text/css"> body { background-color: #9DC0E4; } table.Details { width: 550px; margin-left: -275px; left: 50%; position: absolute; } table.Details tbody.Group { border-bottom: solid black 2px; margin-bottom: 15px; } table.Details th.Group { font-size: x-large; border-bottom: dashed 1px navy; } table.Details th.Name { text-align: left; } table.Details td.Value { text-align: right; } </style> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <% var computer = new ComputerInfo(); using (var iis = Process.GetCurrentProcess()) using (var cpu = new PerformanceCounter("Processor","% Processor Time","_Total")) using (var uptime = new PerformanceCounter("System","System Up Time")) { cpu.NextValue(); uptime.NextValue(); %> <table class="Details"> <tbody class="Group"> <tr> <th class="Group" colspan="2">Environment</th> </tr> <tr> <th class="Name">Local server time</th> <td class="Value"> <%= DateTime.Now.ToString("F")%></td> </tr> <tr> <th class="Name">OS</th> <td class="Value"> <%= computer.OSFullName%><br /> <%= Environment.OSVersion.ToString()%></td> </tr> <tr> <th class="Name">Machine name</th> <td class="Value"> <%= Environment.MachineName%></td> </tr> <tr> <th class="Name">User name</th> <td class="Value"> <%= Environment.UserName%></td> </tr> <tr> <th class="Name">Windows domain</th> <td class="Value"> <%= Environment.UserDomainName%></td> </tr> </tbody> <tbody class="Group"> <tr> <th class="Group" colspan="2">IIS</th> </tr> <tr> <th class="Name">IIS Uptime</th> <td class="Value"> <%= (DateTime.Now- iis.StartTime).ToApproximateString()%></td> </tr> <tr> <th class="Name">Priority</th> <td class="Value"> <%= iis.PriorityClass%></td> </tr> <tr> <th class="Name">Physical Memory Used</th> <td class="Value"> <%= ToSizeString(iis.WorkingSet64)%></td> </tr> <tr> <th class="Name">Virtual Memory Used</th> <td class="Value"> <%= ToSizeString(iis.VirtualMemorySize64)%></td> </tr> </tbody> <tbody class="Group"> <tr> <th class="Group" colspan="2">Hardware</th> </tr> <tr> <th class="Name">Processors</th> <td class="Value"> <%= Environment.ProcessorCount.ToString()%></td> </tr> <tr> <th class="Name">Physical memory</th> <td class="Value"> <%= ToSizeString(computer.TotalPhysicalMemory)%></td> </tr> <tr> <th class="Name">Virtual memory</th> <td class="Value"> <%= ToSizeString(computer.TotalVirtualMemory)%></td> </tr> </tbody> <tbody class="Group"> <tr> <th class="Group" colspan="2">Performance</th> </tr> <tr> <th class="Name">Uptime</th> <td class="Value"> <%= TimeSpan.FromSeconds(uptime.NextValue()).ToApproximateString()%> </td> </tr> <tr> <th class="Name">CPU Usage</th> <td class="Value"> <%= (cpu.NextValue()/100).ToString("p")%> </td> </tr> <tr> <th class="Name">Physical memory free</th> <td class="Value"> <%= ToSizeString(computer.AvailablePhysicalMemory)%></td> </tr> <tr> <th class="Name">Virtual memory free</th> <td class="Value"> <%= ToSizeString(computer.AvailableVirtualMemory)%></td> </tr> </tbody> </table> <%} %> </asp:Content> ToSizeString在.cs文件中定义: protected static string ToSizeString(double bytes) { var culture = CultureInfo.CurrentUICulture; const string format = "#,0.0"; if (bytes < 1024) return bytes.ToString("#,0",culture); bytes /= 1024; if (bytes < 1024) return bytes.ToString(format,culture) + " KB"; bytes /= 1024; if (bytes < 1024) return bytes.ToString(format,culture) + " MB"; bytes /= 1024; if (bytes < 1024) return bytes.ToString(format,culture) + " GB"; bytes /= 1024; return bytes.ToString(format,culture) + " TB"; } ToApproximateString是其他地方定义的扩展方法: public static string ToApproximateString(this TimeSpan time) { if (time.TotalDays > 14) return ((int)(time.TotalDays / 7)).ToString("#,0.0") + " weeks"; if (14 - time.TotalDays < .75) return "two weeks"; if (time.TotalDays > 1) return time.TotalDays.ToString("#,0.0") + " days"; else if (time.TotalHours > 1) return time.TotalHours.ToString("#,0.0") + " hours"; else if (time.TotalMinutes > 1) return time.TotalMinutes.ToString("#,0.0") + " minutes"; else return time.TotalSeconds.ToString("#,0.0") + " seconds"; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – HiddenFor(x => x.Id)正在由UrlParameter而
- asp.net-mvc – ControllerActionInvoker
- asp.net-mvc – 我如何可以渲染局部视图在asp.net mvc 3
- ASP.NET HttpApplication生命周期
- ASP基于B/S模式的失物招领系统
- ASP.NET MVC中将数据从Controller传递到视图
- asp.net-mvc-3 – 从Minifying中跳过单个文件?
- 我可以在Razor中使用@helper语法返回一个字符串吗?
- asp.net-mvc – 捕获文件名作为参数的MVC路由
- asp.net-mvc – 你如何安装Castle Windsor IOC?
推荐文章
站长推荐
- asp.net-mvc-4 – 尽管设置了配置值,但超出了最大
- ASP.Net MVC C#另一个viewmodel中的两个viewmode
- asp.net-mvc-3 – 在web.config文件中忽略resour
- asp.net-mvc-3 – Azure ACS – 最佳实践实施
- 在Entity Framework中使用存储过程(三):逻辑删
- asp.net – [DataType(DataType.EmailAddress)]和
- ASP.NET中下载文件的几种实例代码
- entity-framework – 将DbContext注入FluentVali
- 使用带有asp.net.No错误消息的SalesLogix使用Ole
- .net core 中间件使用
热点阅读