Skip to content

Commit

Permalink
Add deployment ID to debug data in HTML comment
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianEdwards committed Jun 24, 2016
1 parent 18714ea commit 8be10dc
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
84 changes: 84 additions & 0 deletions src/live.asp.net/Services/DeploymentEnvironment.cs
@@ -0,0 +1,84 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;

namespace live.asp.net.Services
{
public class DeploymentEnvironment : IDeploymentEnvironment
{
private readonly string _contentRoot;
private readonly ILogger<DeploymentEnvironment> _logger;
private string _commitSha;

public DeploymentEnvironment(IHostingEnvironment hostingEnv, ILogger<DeploymentEnvironment> logger)
{
_contentRoot = hostingEnv.ContentRootPath;
_logger = logger;
}

public string DeploymentId
{
get
{
if (_commitSha == null)
{
LoadCommitSha();
}

return _commitSha;
}
}

private void LoadCommitSha()
{
var kuduActiveDeploymentPath = Path.GetFullPath(Path.Combine(_contentRoot, "..", "deployments", "active"));
try
{
if (File.Exists(kuduActiveDeploymentPath))
{
_logger.LogDebug("Kudu active deployment file found, using it to set DeploymentID");
_commitSha = File.ReadAllText(kuduActiveDeploymentPath) + "(kudu)";
}
else
{
_logger.LogDebug("Kudu active deployment file not found, using git to set DeploymentID");
var git = Process.Start(new ProcessStartInfo
{
FileName = "git",
Arguments = "rev-parse HEAD",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
});
var gitOut = "";
while (!git.StandardOutput.EndOfStream)
{
gitOut += git.StandardOutput.ReadLine();
}
gitOut += " (local)";

git.WaitForExit();
if (git.ExitCode != 0)
{
_logger.LogDebug("Problem using git to set deployment ID:\r\n git exit code: {0}\r\n git output: {1}", git.ExitCode, _commitSha);
_commitSha = "(Could not determine deployment ID)";
}
else
{
_commitSha = gitOut;
}
}
}
catch (Exception ex)
{
_logger.LogError(0, ex, "Error determining deployment ID");
_commitSha = "(Error determining deployment ID)";
}
}
}
}
10 changes: 10 additions & 0 deletions src/live.asp.net/Services/IDeploymentEnvironment.cs
@@ -0,0 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace live.asp.net.Services
{
public interface IDeploymentEnvironment
{
string DeploymentId { get; }
}
}
2 changes: 2 additions & 0 deletions src/live.asp.net/Startup.cs
Expand Up @@ -67,6 +67,8 @@ public void ConfigureServices(IServiceCollection services)

services.AddScoped<IShowsService, YouTubeShowsService>();

services.AddSingleton<IDeploymentEnvironment, DeploymentEnvironment>();

if (string.IsNullOrEmpty(Configuration["AppSettings:AzureStorageConnectionString"]))
{
services.AddSingleton<ILiveShowDetailsService, FileSystemLiveShowDetailsService>();
Expand Down
2 changes: 2 additions & 0 deletions src/live.asp.net/Views/Shared/_Layout.cshtml
@@ -1,4 +1,5 @@
@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment HostingEnvironment
@inject live.asp.net.Services.IDeploymentEnvironment DeploymentEnvironment

<!DOCTYPE html>
<html>
Expand Down Expand Up @@ -63,6 +64,7 @@
<cache>
<!-- Environment: @HostingEnvironment.EnvironmentName -->
<!-- Application: @Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationName, @Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationVersion -->
<!-- Deployment: @DeploymentEnvironment.DeploymentId -->
<!-- Framework: @Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.RuntimeFramework -->
<!-- Runtime: @Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Runtime.RuntimeType, @Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Runtime.RuntimeArchitecture, @Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Runtime.RuntimeVersion -->
<!-- OS: @Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Runtime.OperatingSystem @Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Runtime.OperatingSystemVersion -->
Expand Down
2 changes: 1 addition & 1 deletion src/live.asp.net/appsettings.json
Expand Up @@ -18,7 +18,7 @@
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
Expand Down

2 comments on commit 8be10dc

@addisu
Copy link
Owner

@addisu addisu commented on 8be10dc Jun 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to use this code, I am getting this error on my page ..

'PlatformServices' does not contain a definition for 'Runtime' and no extension method 'Runtime' accepting a first argument of type 'PlatformServices' could be found (are you missing a using directive or an assembly reference?)

@DamianEdwards
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.