Determine runtime of Windows Server via ASP.Net - asp.net

is there a built-in property in any of the ASP.net classes that determines what the uptime of the webserver, where the webapplication is running on, is?
These are some of the possibilities that popped into my mind:
global.asax and the Application_OnStart Event. Get the timestamp and to determine the uptime, I just substract it from DateTime.Now
Write a windows service (automatically startup) which's only purpose is to store the value of the service start somewhere
But neither of these two offer a "built-in" solution. Is there something I might oversee in the .net framework?

To get system uptime, use the following code:
TimeSpan uptime;
using (var uptimeCounter = new PerformanceCounter("System", "System Up Time")) {
uptimeCounter.NextValue();
uptime = TimeSpan.FromSeconds(uptimeCounter.NextValue());
}
EDIT: Note that this can't be used by partially trusted code.
You could use TimeSpan.FromMilliseconds(Environment.TickCount), but it'll wrap after two weeks.
I wrote a server status page in ASP.Net which shows server uptime and more.
Here is the entire page:
<%# 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 is defined in the .cs file:
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 is an extension method defined elsewhere:
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";
}

Thanks, that was the kind of answer I was looking for =)
I'll stick to
TimeSpan _diff = DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime);
this.litRuntime.Text = string.Format("{0} days and {1:00}:{2:00}:{3:00}", (int)_diff.TotalDays, _diff.Hours, _diff.Minutes, _diff.Seconds);
for the moment.

Related

Google sheets app script fails to process CSS styling

I have an image in google spreadsheet. Pressing the button fires the script which is supposed to open a stylized table. Instead, it opens a table and fails to apply CSS style to it. I read Google documentation on making a separate file for styling and putting an include in the main HTML file as follows
// <?!= include('stylesheet'); ?>
I did this. I get a table with with being a plain text.
How to make the script actually process CSS styles?
gs file:
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
};
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('Stundu_laiki')
.setHeight(600);
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'Stundu laiki')
}
stylesheet.html file with CSS for tables
<style>
.w3-table,.w3-table-all{}.w3-table-all{border:1px solid #ccc}
.w3-table-lined tr:nth-child(odd){background-color:#fff}.w3-table-lined tr:nth-child(even){background-color:#f1f1f1}
.w3-table-all tr:nth-child(odd){background-color:#fff}.w3-table-all tr:nth-child(even){background-color:#f1f1f1}
.w3-bordered tr,.w3-table-all tr{border-bottom:1px solid #ddd}.w3-striped tbody tr:nth-child(even){background-color:#f1f1f1}
</style>
stundu_laiki.html with a table
<!DOCTYPE html>
<html>
<head>
<base target="_top">
**<?!= include('stylesheet'); ?>**
</head>
<body>
<table width="98%" border="1" cellpadding="3px" cellspacing="0" style="w3-table-all">
<tr>
<td width="23%">1.</td>
<td width="77%">8:30 - 9:10</td>
</tr>
<tr>
<td>2.</td>
<td>9:20 - 10:00</td>
</tr>
<tr>
<td>3.</td>
<td>10:10 - 10:50</td>
</tr>
<tr>
<td>4.</td>
<td>11:05 - 11:45</td>
</tr>
<tr>
<td>5.</td>
<td>12:00 - 12:40</td>
</tr>
<tr>
<td>6.</td>
<td>13:10 - 13:50</td>
</tr>
<tr>
<td>7.</td>
<td> 14:00 - 14:40</td>
</tr>
<tr>
<td>8.</td>
<td>14:50 - 15:30</td>
</tr>
<tr>
<td>9.</td>
<td>15:35 - 16:15</td>
</tr>
</table>
<h2> Saīsināto dienu stundu laiki</h2>
<table width="98%" border="1" cellpadding="3px" cellspacing="0" class="w3-table-all">
<tr>
<td width="23%">1.</td>
<td width="77%">8:30 - 9:00</td>
</tr>
<tr>
<td>2.</td>
<td>9:10 - 9:40</td>
</tr>
<tr>
<td>3.</td>
<td>9:50 - 10:20</td>
</tr>
<tr>
<td>4.</td>
<td>10:35 - 11:05</td>
</tr>
<tr>
<td>5.</td>
<td>11:20 - 11:50</td>
</tr>
<tr>
<td>6.</td>
<td>12:20 - 12:50</td>
</tr>
<tr>
<td>7.</td>
<td> 13:00 - 13:30</td>
</tr>
<tr>
<td>8.</td>
<td>13:40 - 14:10</td>
</tr>
<tr>
<td>9.</td>
<td>14:15 - 14:45</td>
</tr>
</table>
</body>
</html>
Use createTemplateFromFile instead then evaluate your html. See final script below.
Final script:
function openDialog() {
var html = HtmlService.createTemplateFromFile('stundu_laiki')
.evaluate()
.setHeight(600);
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'Stundu laiki')
}
Output:

Is it possible to fix <footer> in bootstraptable?

i found the showFooter Property. but i do not get how to customize the Content for that.
i only need to fill three of total 8 in ...
so table Looks basically like this:
<div class="table-responsive">
<table class="table table-bordered" id="tblKontoauszug">
<thead>
<tr>
<th data-sortable="#sortable" data-sorter="dateSorter">Buchungsdat.</th>
<th data-sortable="#sortable">Belegnr.</th>
<th data-sortable="#sortable">BA</th>
<th data-sortable="#sortable" data-sorter="betragSorter">Betrag</th>
<th data-sortable="#sortable">Buchungstext</th>
<th data-sortable="#sortable">Gegenkontoart</th>
<th data-sortable="#sortable">Gegenkonto</th>
<th data-sortable="#sortable">Bezeichnung</th>
</tr>
<tr class="info text-bold">
<td>
#if ( Model.Zeilen.Count() > 0 )
{
<span>#Model.Zeilen.Min(b => b.Buchungsdatum).ToShortDateString()</span>
}
</td>
<td colspan="2">Anfangsbestand</td>
<td class="text-right">#Model.Anfangsbestand.ToString("N")</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
<tfoot>
<tr class="info text-bold">
<td>
#if ( Model.Zeilen.Count() > 0 )
{
<span>#Model.Zeilen.Max( b => b.Buchungsdatum ).ToShortDateString()</span>
}
</td>
<td colspan="2">Endbestand</td>
<td class="text-right #( Model.Endbestand < 0 ? "negative" : "")">#Model.Endbestand.ToString( "N" )</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
<tbody>
#foreach ( var zeile in Model.Zeilen )
{
<tr>
<td>#zeile.Buchungsdatum.ToShortDateString()</td>
<td>#zeile.Belegnummer</td>
<td title="#zeile.BelegartText">#zeile.Belegart</td>
<td class="text-right #( zeile.Betrag < 0 ? "negative" : "")">#zeile.Betrag.ToString("N")</td>
<td>#zeile.Buchungstext</td>
<td>#zeile.Gegenkontoart</td>
<td>#zeile.Gegenkonto</td>
<td>#zeile.Bezeichnung</td>
</tr>
}
</tbody>
</table>
</div>
You can customize content in the footer by adding the data-footer-formatter attribute to the column you want to add the footer to and setting that attribute equal to a defined javascript function. Here's an example:
HTML:
<table data-toggle="table" data-show-footer="true" data-footer-style="footerStyle" data-show-columns="true">
<thead>
<tr>
<th data-sortable="true" data-footer-formatter="totalText">Name</th>
<th data-sortable="true" data-footer-formatter="carsSum"># Cars</th>
</tr>
</thead>
<tbody>
<tr>
<td>Johnny</td>
<td>2</td>
</tr>
<tr>
<td>Zack</td>
<td>3</td>
</tr>
<tr>
<td>Timmy</td>
<td>2</td>
</tr>
</tbody>
</table>
Javascript:
function totalText(data) {
return 'TOTAL';
}
function carsSum(data) {
return '7';
}
function footerStyle(value, row, index) {
return {
css: { "font-weight": "bold" }
};
}
Notice I'm not using <tfoot> at all here. That is because <tfoot> won't work with the show columns dropdown list but this will (if I were to use the data-show-columns="true" attribute on the <table> element as I did in the example above). You can also style the footer using the data-footer-style attribute. In my example, I'm making all text in the footer bold.
You can see the code in action here: https://jsfiddle.net/3mou92px/9/

I want to page break in pdf reporting from mvc3 razor view to pdf

I am working in mvc3 razor view. I create pdf report from mvc3 razor view below is my controller code.
public ActionResult PReqDocPrint()
{
List<PReqPrintModel> Rec = new List<PReqPrintModel> { };
Rec = PReqPrint("", "", "");
return this.ViewPdf("Purchase Requisition", "PReqDocPrint", Rec);
}
I get data from database through stored procedure and return to controller
and my view code is given below.
#model IEnumerable<CBS.Models.PReqPrintModel>
#{
Layout = "~/Views/Shared/_LayoutForPrints.cshtml";
}
<table border="1" >
<tr>
<td>
<h2 style="text-align:center; font-family:Times New Roman;">Purchase Requisition</h2>
</td>
</tr>
</table>
<div style="color:Black;border-width:0px 1px 1px 0px;font-size:8px; font-weight:normal; page-break-before:always;">
#{
var OddColor = "#aaaaff";
var evenColor = "#EEFFFF";
var Odd = OddColor;
var eve = evenColor;
}
<table style="font-family:Times New Roman; font-size:small">
#foreach (var doc in Model.GroupBy(d => d.PReqNo))
{
foreach (var pre in doc)
{
<tr style="border:1;">
<td>
Purchase Requisition: #Html.Encode(pre.PReqNo)
</td>
<td>
Purchase Date: #String.Format("{0:dd/MM/yyyy}", #Html.Encode(pre.PReqDate.Date))
</td>
</tr>
<tr style="border:1;">
<td>
Store Location: #Html.Encode(pre.StName)
</td>
<td>
Department: #Html.Encode(pre.DeptName)
</td>
</tr>
<tr style="border:1;">
<td>
Remarks: #Html.Encode(pre.RefRemarks)
</td>
<td></td>
</tr>
}
<tr >
<td colspan="2">
<table border="1" style="color:Black;border-width:0px 1px 1px 0px;font-size:8px;font-family:Helvetica;font-weight:normal;">
<tr style="background-color:#7070a0; font-weight:bold; color:Black; border:1;" >
<td align="left">
Code
</td>
<td align="left">
Description
</td>
<td align="left">
Unit
</td>
<td align="left">
Part No
</td>
<td align="left">
Lot No
</td>
<td align="left">
Item Remarks
</td>
<td align="right">
Quantity
</td>
<td align="right">
Amount
</td>
</tr>
#foreach (var item in doc)
{
<tr bgcolor="#Odd">
<td align="left">
#Html.Encode(item.ItemCode)
</td>
<td align="left">
#Html.Encode(item.ItemDesc)
</td>
<td align="left">
#Html.Encode(item.Units)
</td>
<td align="left">
#Html.Encode(item.PartNo)
</td>
<td align="left">
#Html.Encode(item.LotNo)
</td>
<td align="left">
#Html.Encode(item.Remarks)
</td>
<td align="right">
#Html.Encode(item.QtyIN)
</td>
<td align="right">
#Html.Encode(item.Qty)
</td>
</tr>
}
if (Odd == OddColor)
{
Odd = evenColor;
}
else
{
Odd = OddColor;
}
}
</table>
</td>
</tr>
}
</table>
</div>
<div class="break"></div>
Below is my PDF result image which return from view
Here I want to start new PDF page When second Purchase Requisition no 2014030001 start. Any one please help me.
i already try inline CSS for page break or table page-break-before:always; and also try a separate div tag with page-break-before:always; but nothing obtain my required result.
Also it not apply separate style sheet formats on PDF view.

asp.net mvc3 razor syntax convert

How do I convert this gridview in razor syntax, I have curly brackets inside (data => { %>) ?
<%Html.GridView<Employee>(
Model,
data => { %>
<table class="grid" cellpadding="0" cellspacing="0">
<tr>
<th> </th>
<th> </td>
<th> </td>
<th>Name</th>
<th>E-mail</th>
</tr>
<% },
(item, css) => { %>
<tr class="<%=css%>">
<td><%=Html.ActionImage("Edit", "Home", new { Id = item.Id }, "~/Content/edit.gif", "Edit")%></td>
<td><%=Html.ActionImage("Delete", "Home", new { Id = item.Id }, "~/Content/delete.gif", "Delete")%></td>
<td> </td>
<td><%=item.Name%></td>
<td><%=item.Email%></td>
</tr>
<% },
"item",
"item-alternating",
item => { %>
<%using (Html.BeginForm("Save", "Home", new { Id = item.Id }, FormMethod.Post, new { id = "editForm" })) {%>
<tr class="item-edit">
<td><input type="image" runat="server" id="save" src="~/Content/ok.gif" alt="Update" /></td>
<td><%=Html.ActionImage("Index", "Home", null, "~/Content/cancel.gif", "Cancel")%></td>
<td> </td>
<td><%=Html.TextBox("Name", item.Name)%></td>
<td><%=Html.TextBox("Email", item.Email)%></td>
</tr>
<% } %>
<% },
data => { %>
<tr>
<td colspan="5"><hr /></td>
</tr>
<tr class="paging">
<td colspan="5">
<% if (data.PagedList.IsPreviousPage) { %>
<%=Html.ActionImage("Show", "Home", new { page = data.PagedList.PageIndex - 1 }, "~/Content/previous.gif", "Previous page")%>
<% } %>
<%=data.PagedList.TotalCount.ToString()%> records
<% if (data.PagedList.IsNextPage) { %>
<%=Html.ActionImage("Show", "Home", new { page = data.PagedList.PageIndex + 1 }, "~/Content/next.gif", "Next page")%>
<% } %>
</td>
</tr>
<tr>
<td colspan="5"> </td>
</tr>
<%using (Html.BeginForm("Add", "Home", FormMethod.Post, new { id = "addForm" })) {%>
<tr>
<th> </th>
<th> </td>
<th> </td>
<th>Name</th>
<th>E-mail</th>
</tr>
<tr>
<td><input type="image" runat="server" id="add" src="~/Content/add.gif" alt="Add" /></td>
<td> </td>
<td> </td>
<td><%=Html.TextBox("Name", "")%></td>
<td><%=Html.TextBox("Email", "")%></td>
</tr>
<% } %>
</tr>
</table>
<% });%>
Razor replaces the brackets <% ... %> with just the # symbol. So for example, this line
<%=Html.TextBox("Email", "")%>
becomes
#Html.TextBox("Email", "")
That's basically it. You'll just have to remove ALL of the <% ... %> brackets.

Need help with Razor syntax in cart index view (Razor view engine)

At first use I loved it and it felt so much less cluttered then the webforms <%: %> etc. view engine,
but upon using it further I can't help but notice it's hypersensitive to where its "{" parentheses are placed and other scenarios. It gives errors at points where the older view engine was not as picky.
For example the below code will produce an error because the form helper closing bracket }
is under the </table> tag. If I place it higher above the </tbody> it works! But I don't need
it there because the submit button input has to be nested within and I don't want to put the button input into the table.
#model CartTest.Models.Cart
#{
ViewBag.Title = "Index";
}
<h2>Cart Index</h2>
<table width="80%" align="center">
<thead>
<tr>
<th align="center">Quantity</th>
<th align="left">Item</th>
<th align="right">Price</th>
<th align="right">Subtotal</th>
</tr>
</thead>
<tbody>
#{int index = 0;}
#using (Html.BeginForm("UpdateCart","Cart"))
{
foreach (var line in Model.Lines)
{
<tr>
#Html.Hidden("Lines.Index", index)
<td align="center">#Html.TextBox("Lines[" + index + "].Quantity", line.Quantity)</td>
<td align="left">#line.Product.Name</td>
<td align="right">#line.Product.Price</td>
<td align="right">#(line.Quantity * line.Product.Price)</td>
<td align="right">#Html.ActionLink("Remove", "RemoveItem", new { productId = line.Product.ProductID }, null)</td>
</tr>
index++;
}
</tbody>
<tfoot></tfoot>
</table>
<input type="submit" value="Update Cart" />
}
The reason it works above the </tbody> is because you declared the BeginForm inside the opening <tbody>. They must be nested properly in order to work. If you don't want to put the input button inside the table, then move the BeginForm outside the table element so the opening and closing brace are at the same level.
#using (Html.BeginForm("UpdateCart","Cart"))
{
<table width="80%" align="center">
<thead><tr>
<th align="center">Quantity</th>
<th align="left">Item</th>
<th align="right">Price</th>
<th align="right">Subtotal</th>
</tr></thead>
<tbody>
#{int index = 0;}
foreach (var line in Model.Lines)
{
<tr>
#Html.Hidden("Lines.Index", index)
<td align="center">#Html.TextBox("Lines[" + index + "].Quantity", line.Quantity)</td>
<td align="left">#line.Product.Name</td>
<td align="right">#line.Product.Price</td>
<td align="right">#(line.Quantity * line.Product.Price)</td>
<td align="right">#Html.ActionLink("Remove", "RemoveItem", new { productId = line.Product.ProductID }, null)</td>
</tr>
index++;
}
</tbody>
<tfoot>
</tfoot>
</table>
<input type="submit" value="Update Cart" />
}
Move the
#{int index = 0;}
#using (Html.BeginForm("UpdateCart","Cart"))
{
part up above your table, so the whole table is inside the form tag.

Resources