Skip to content

Application Status

Mark Hazleton edited this page Jun 16, 2021 · 1 revision

Overview

For the web application, it is a good idea to have a status page that can be used for health checks and other system monitoring tools. The status is a quick way to know what version of the code is running.

The current results of the /status end point is as follows:

{
"buildVersion": 
{
   "build": 0,
   "buildDate": "2021-05-30T16:59:01",
   "majorVersion": 1,
   "minorVersion": 3,
   "revision": 0
},
"features": {},
"messages": [],
"region": null,
"status": 2,
"tests": 
{
   "Employee Database": "Success"
}
}

As the complexity of an application grows, you can add additional tests and messages to the in the three list of strings (features, messages,tests).

AssemblyInformationalVersionAttribute

Source: https://www.meziantou.net/getting-the-date-of-build-of-a-dotnet-assembly-at-runtime.htm Using the .NET SDK, you can use an MSBuild property to define the value of the AssemblyInformationalVersionAttribute. So, you can inject the value of DateTime.UtcNow in the value of this attribute and then parse it at runtime to get the build date.

Open the csproj and add the following line:

<Project ToolsVersion="15.0" Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <SourceRevisionId>build$([System.DateTime]::UtcNow.ToString("yyyyMMddHHmmss"))</SourceRevisionId>
  </PropertyGroup>
</Project>

The value of SourceRevisionId is added to the version in the metadata section of the version (after the +). The value is of the following form: 1.2.3+build20180101120000

private static DateTime GetBuildDate(Assembly assembly)
{
    const string BuildVersionMetadataPrefix = "+build";

    var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
    if (attribute?.InformationalVersion != null)
    {
        var value = attribute.InformationalVersion;
        var index = value.IndexOf(BuildVersionMetadataPrefix);
        if (index > 0)
        {
            value = value.Substring(index + BuildVersionMetadataPrefix.Length);
            if (DateTime.TryParseExact(value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
            {
                return result;
            }
        }
    }

    return default;
}

You can then get the build date using:

GetBuildDate(Assembly.GetExecutingAssembly());
Clone this wiki locally