Skip to content

Commit

Permalink
Causing failure if logging cannot succeed
Browse files Browse the repository at this point in the history
  • Loading branch information
sburmanoctopus committed Dec 12, 2023
1 parent 032f5ba commit bd04859
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public PollingTentacleBuilder(int pollingPort, string serverThumbprint)

ServerThumbprint = serverThumbprint;
}

internal async Task<RunningTentacle> Build(ILogger log, CancellationToken cancellationToken)
{
var instanceName = InstanceNameGenerator();
Expand All @@ -46,7 +46,7 @@ internal async Task<RunningTentacle> Build(ILogger log, CancellationToken cancel
cancellationToken);
}

private void ConfigureTentacleToPollOctopusServer(string configFilePath, Uri subscriptionId, string applicationDirectory)
void ConfigureTentacleToPollOctopusServer(string configFilePath, Uri subscriptionId, string applicationDirectory)
{
WithWritableTentacleConfiguration(configFilePath, writableTentacleConfiguration =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using System;
using Serilog;

namespace Octopus.Tentacle.Tests.Integration.Support.SetupFixtures
{
public static class TentacleNLogFile
{
public static void TryAdjustForRuntime(TentacleRuntime runtime, ILogger logger, Func<string, string> adjustLogContents)
{
try
{
var exePath = TentacleExeFinder.FindTentacleExe(runtime);
var exeFileInfo = new FileInfo(exePath);
var nlogFileInfo = new FileInfo(Path.Combine(exeFileInfo.DirectoryName!, "Tentacle.exe.nlog"));

var content = File.ReadAllText(nlogFileInfo.FullName);
content = adjustLogContents(content);
File.WriteAllText(nlogFileInfo.FullName, content);
}
catch (Exception e)
{
logger.Error(e, $"Unable to turn on Trace logging for {runtime}");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Serilog;

namespace Octopus.Tentacle.Tests.Integration.Support.SetupFixtures
{
public class TurnOnExceptionThrowingForLogFileForLatestTentacle : ISetupFixture
{
public void OneTimeSetUp(ILogger logger)
{
// This will help us find errors that normally get swallowed, and become hard to find.
TryTurnOnExceptionThrowingForTentacleRuntime(TentacleRuntime.DotNet6, logger);
TryTurnOnExceptionThrowingForTentacleRuntime(TentacleRuntime.Framework48, logger);
}

static void TryTurnOnExceptionThrowingForTentacleRuntime(TentacleRuntime runtime, ILogger logger)
{
TentacleNLogFile.TryAdjustForRuntime(
runtime,
logger,
content => content.Replace("throwExceptions=\"false\"", "throwExceptions=\"true\""));
}

public void OneTimeTearDown(ILogger logger)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,22 @@ namespace Octopus.Tentacle.Tests.Integration.Support.SetupFixtures
{
public class TurnOnTraceLoggingForLogFileForLatestTentacle : ISetupFixture
{
private CancellationTokenSource cts = new();

public void OneTimeSetUp(ILogger logger)
{
TryTurnOnTraceLoggingForTentacleRuntime(TentacleRuntime.DotNet6, logger);
TryTurnOnTraceLoggingForTentacleRuntime(TentacleRuntime.Framework48, logger);
}

void TryTurnOnTraceLoggingForTentacleRuntime(TentacleRuntime runtime, ILogger logger)
static void TryTurnOnTraceLoggingForTentacleRuntime(TentacleRuntime runtime, ILogger logger)
{
try
{
var exePath = TentacleExeFinder.FindTentacleExe(runtime);
var exeFileInfo = new FileInfo(exePath);
var nlogFileInfo = new FileInfo(Path.Combine(exeFileInfo.DirectoryName!, "Tentacle.exe.nlog"));

var content = File.ReadAllText(nlogFileInfo.FullName);
content = content.Replace("<logger name=\"*\" minlevel=\"Info\" writeTo=\"octopus-log-file\" />", "<logger name=\"*\" minlevel=\"Trace\" writeTo=\"octopus-log-file\" />");
File.WriteAllText(nlogFileInfo.FullName, content);
}
catch (Exception e)
{
logger.Error(e, $"Unable to turn on Trace logging for {runtime}");
}
TentacleNLogFile.TryAdjustForRuntime(
runtime,
logger,
content => content.Replace("<logger name=\"*\" minlevel=\"Info\" writeTo=\"octopus-log-file\" />", "<logger name=\"*\" minlevel=\"Trace\" writeTo=\"octopus-log-file\" />"));
}

public void OneTimeTearDown(ILogger logger)
{
cts.Cancel();
cts.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,16 @@ public async Task ShouldLogStartupDiagnosticsToInstanceLogFileOnly(TentacleConfi
{
await using var clientAndTentacle = await tc.CreateBuilder().Build(CancellationToken);
var startingLogText = clientAndTentacle.RunningTentacle.ReadAllLogFileText();


var fs = new FileStream(clientAndTentacle.RunningTentacle.LogFilePath, FileMode.Open,
FileAccess.ReadWrite, FileShare.None);


var (exitCode, stdout, stderr) = await RunCommandAndAssertExitsWithSuccessExitCode(tc, "show-thumbprint", $"--instance={clientAndTentacle.RunningTentacle.InstanceName}");

fs.Dispose();

try
{
var logFileText = Policy
Expand All @@ -425,7 +432,6 @@ public async Task ShouldLogStartupDiagnosticsToInstanceLogFileOnly(TentacleConfi
{
throw new NotLoggedYetException();
}

return newLog;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ namespace Octopus.Tentacle.Tests.Integration
[SetUpFixture] // Must be the one and only.
public class TentacleIntegrationSetupFixtures
{
private ISetupFixture[] setupFixtures = new ISetupFixture[]
readonly ISetupFixture[] setupFixtures =
{
new TurnOnTraceLoggingForLogFileForLatestTentacle(),
new TurnOnExceptionThrowingForLogFileForLatestTentacle(),
new BumpThreadPoolForAllTests(),
new WarmTentacleCache()
};
Expand Down

0 comments on commit bd04859

Please sign in to comment.