forked from aspnet/live.asp.net
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deployment ID to debug data in HTML comment
- Loading branch information
1 parent
18714ea
commit 8be10dc
Showing
5 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8be10dc
There was a problem hiding this comment.
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?)
8be10dc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See aspnet/Announcements#191