DEV Community

Ricardo
Ricardo

Posted on • Originally published at rmauro.dev on

Add Build Time to your C# Assembly

Add Build Time to your C# Assembly

The build time can be useful sometimes. For instance, in a web application, we can have the version and the date that was made the build. Such as version 1.1.0 build at 2021-04-21 23:11:00.

Prelude

Often in our developer, DevOps or infrastructure life we find ourselves wondering:

  • Is this the right assembly?_
  • Why the changes are not there? What happened?_
  • Which version is this? Is this the latest version?_
  • Oh man, why is this not working? Oh my gosh. I hate this... ahahhahaha_

This happens especially when we don't have control over the deployment and it's published by a third-party professional.

By default, DotNet doesn't provide us that. Let's get started!

This works in .Net Core 3.1 and .NET 5.

To make this happen we're going to tell .NET compiler while building the code to also write in the assembly SourceRevisionId tag the System.DateTime.Now or System.DateTime.UtcNow.

Adding Build Time to Our Assembly

Add to the CSPROJ file the tag SourceRevisionId with the following content.



<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
    </PropertyGroup>

    <PropertyGroup>
        <SourceRevisionId>build$([System.DateTime]::UtcNow.ToString("yyyy-MM-ddTHH:mm:ss:fffZ"))</SourceRevisionId>
    </PropertyGroup>

</Project>



Enter fullscreen mode Exit fullscreen mode
App.csproj

This works for Console App , Windows Forms , WPF and Web App. Xamarin and others I have no clue about.

Leave a comment if you know.

To get the written date using C# create the method GetLinkerTime() as described next.



using System;
using System.Globalization;
using System.Reflection;

public static DateTime GetLinkerTime(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[(index + BuildVersionMetadataPrefix.Length)..];
            return DateTime.ParseExact(value, "yyyy-MM-ddTHH:mm:ss:fffZ", CultureInfo.InvariantCulture);
        }
    }

    return default;
}


Enter fullscreen mode Exit fullscreen mode
Method GetLinkerTime

Full Application - Sample Console Application



using System;
using System.Globalization;
using System.Reflection;

namespace BuildTimeApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var buildTime = GetLinkerTime(Assembly.GetEntryAssembly());
            Console.WriteLine($"Build time at {buildTime}");

            Console.WriteLine("Write any key to close");
            Console.ReadKey();
        }

        public static DateTime GetLinkerTime(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[(index + BuildVersionMetadataPrefix.Length)..];
                    return DateTime.ParseExact(value, "yyyy-MM-ddTHH:mm:ss:fffZ", CultureInfo.InvariantCulture);
                }
            }
            return default;
        }
    }
}


Enter fullscreen mode Exit fullscreen mode
Sample Program.cs

Sample output.

Add Build Time to your C# Assembly
sample output

Source Code

https://github.com/ricardodemauro/buildTimeConsoleApp-tutorial

Top comments (0)