Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get current running version of dotnet core runtime

Tags:

c#

.net-core

I have an aspnetcore webapp and I'd like it to write it's current version, as well as the dotnet core runtime version that it's running on to it's log when it starts up.

I'd like to do this as my webapp runs on various VM's in the cloud and I'd like to be able to look at the logs from all of them and make sure they're all running the same dotnet core runtime version.

What I want is something like this.

App version 1.0.1 running on dotnet 2.0.6

Getting my app version is easy (just assembly version), However, I can't find a way to get the dotnet runtime version?

I've seen various things referencing the Microsoft.DotNet.PlatformAbstractions nuget package, however this doesn't appear to give me the dotnet runtime version at all.

There's also System.Environment.Version, but that reports 4.0.30319.42000 which is the "desktop" dotnet 4.6+ framework version, not the dotnet core version.

like image 263
Orion Edwards Avatar asked Mar 15 '18 21:03

Orion Edwards


People also ask

How do I find the runtime version of .NET Core?

You can see both the SDK versions and runtime versions with the command dotnet --info . You'll also get other environmental related information, such as the operating system version and runtime identifier (RID).

How do I know what version of runtime I have?

Finding the version by using the Runtime menu On the Runtime menu, select About. The version is displayed in the Version field in the About dialog box.

What is the runtime of .NET Core?

NET releases in odd-numbered years are Long-term Support (LTS) releases and are supported for three years. Releases in even-numbered years are Short-term Support (STS) releases and are supported for 18 months.


Video Answer


2 Answers

Since .NET Core 3.0, you can directly call improved API to get such information.

var netCoreVer = System.Environment.Version; // 3.0.0
var runtimeVer = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription; // .NET Core 3.0.0-preview4.19113.15

Check out this issue

like image 170
Jerry Bian Avatar answered Oct 19 '22 02:10

Jerry Bian


For a detailed description you can find the original article here: https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed

Along with the original github comment chain here: https://github.com/dotnet/BenchmarkDotNet/issues/448

public static string GetNetCoreVersion() {
  var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
  var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
  int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
  if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
    return assemblyPath[netCoreAppIndex + 1];
  return null;
}
like image 34
musicinmyhead Avatar answered Oct 19 '22 03:10

musicinmyhead