Skip to content

Instantly share code, notes, and snippets.

@ShawInnes
Last active November 18, 2021 07:10
Show Gist options
  • Save ShawInnes/1adfa25d3b90e04c30fd9ed327fe7a88 to your computer and use it in GitHub Desktop.
Save ShawInnes/1adfa25d3b90e04c30fd9ed327fe7a88 to your computer and use it in GitHub Desktop.
Add Metadata to ASP.NET Project
public class MetadataHealthCheck : IHealthCheck
{
private readonly string? _commitHash;
private readonly string? _product;
private readonly string? _version;
private readonly string? _commonVersion;
private readonly string? _fileVersion;
private readonly string _timeZoneName;
private readonly string _currentCulture;
private readonly string _machineName;
public MetadataHealthCheck()
{
var entryAssembly = Assembly.GetEntryAssembly();
if (entryAssembly != null)
{
_version = entryAssembly.GetName().Version?.ToString();
_fileVersion = entryAssembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version;
_product = entryAssembly.GetCustomAttribute<AssemblyProductAttribute>()?.Product;
_commitHash = entryAssembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
}
_commonVersion = typeof(MetadataHealthCheck).Assembly.GetName().Version?.ToString();
_timeZoneName = TimeZoneInfo.Local.Id;
_currentCulture = Thread.CurrentThread.CurrentCulture.Name;
_machineName = Environment.MachineName;
}
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken()) =>
Task.FromResult(HealthCheckResult.Healthy(_version, new Dictionary<string, object>
{
{"Version", _version ?? "unknown"},
{"CommonVersion", _commonVersion ?? "unknown"},
{"FileVersion", _fileVersion ?? "unknown"},
{"Commit", _commitHash ?? "unknown"},
{"MachineName", _machineName},
{"Product", _product ?? "unknown"},
{"ServerTime", DateTime.Now},
{"UtcTime", DateTime.UtcNow},
{"TimeZone", _timeZoneName},
{"Culture", _currentCulture},
{"Currency", $"{123456.78:C}"},
}));
}
# -Company ''
# -Product ''
# -Version $GitVersion_SemVer
# -AssemblyVersion $GitVersion_AssemblySemVer
# -FileVersion $GitVersion_NuGetVersion
# -InformationalVersion $GitVersion_InformationalVersion
Param(
[string] $Company,
[string] $Product,
[bool] $TreatWarningsAsErrors = $False,
[String] $Version,
[String] $AssemblyVersion,
[String] $FileVersion,
[String] $InformationalVersion
)
$xml = New-Object Xml
$projectElement = $xml.CreateElement('Project')
$propertyGroupElement = $xml.CreateElement('PropertyGroup')
if ('' -ne $Company) {
Write-Host "Setting Company $Company"
$companyElement = $xml.CreateElement("Company")
$companyElement.AppendChild($xml.CreateTextNode($Company)) | Out-Null
$propertyGroupElement.AppendChild($companyElement) | Out-Null
}
if ('' -ne $Product) {
Write-Host "Setting Product $Product"
$productElement = $xml.CreateElement("Product")
$productElement.AppendChild($xml.CreateTextNode($Product)) | Out-Null
$propertyGroupElement.AppendChild($productElement) | Out-Null
}
if ($null -ne $TreatWarningsAsErrors) {
Write-Host "Setting TreatWarningsAsErrors $TreatWarningsAsErrors"
$treatWarningsAsErrorsElement = $xml.CreateElement("TreatWarningsAsErrors")
$treatWarningsAsErrorsElement.AppendChild($xml.CreateTextNode($TreatWarningsAsErrors)) | Out-Null
$propertyGroupElement.AppendChild($treatWarningsAsErrorsElement) | Out-Null
}
if ('' -ne $Version) {
Write-Host "Setting Version $Version"
$versionElement = $xml.CreateElement("Version")
$versionElement.AppendChild($xml.CreateTextNode($Version)) | Out-Null
$propertyGroupElement.AppendChild($versionElement) | Out-Null
}
if ('' -ne $AssemblyVersion) {
Write-Host "Setting AssemblyVersion $AssemblyVersion"
$assemblyVersionElement = $xml.CreateElement("AssemblyVersion")
$assemblyVersionElement.AppendChild($xml.CreateTextNode($AssemblyVersion)) | Out-Null
$propertyGroupElement.AppendChild($assemblyVersionElement) | Out-Null
}
if ('' -ne $FileVersion) {
Write-Host "Setting FileVersion $FileVersion"
$fileVersionElement = $xml.CreateElement("FileVersion")
$fileVersionElement.AppendChild($xml.CreateTextNode($FileVersion)) | Out-Null
$propertyGroupElement.AppendChild($fileVersionElement) | Out-Null
}
if ('' -ne $InformationalVersion) {
Write-Host "Setting InformationalVersion $InformationalVersion"
$informationalVersionElement = $xml.CreateElement("InformationalVersion")
$informationalVersionElement.AppendChild($xml.CreateTextNode($InformationalVersion)) | Out-Null
$propertyGroupElement.AppendChild($informationalVersionElement) | Out-Null
}
$projectElement.AppendChild($propertyGroupElement) | Out-Null
$xml.AppendChild($projectElement) | Out-Null
$xml.Save('Directory.Build.props')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment