Reading the output of each task after they are executed during a deployment

I need to read each step’s log output right after they are executed during a deployment and upload the results to the Azure Application Insights for monitoring the metrics such as performance, etc. These metrics will help us see which task took how many minutes, what happened, why happened, etc. I am thinking two options:

1.Add a PowerShell script task that reads the log output of each step after they are executed and upload the result to the Azure. This new task will be added after each task to capture their output.
2. Add a PowerShell script to the end of the deployment that will read the entire log output, parse it, and upload the results to the Application Insights.

However, I am afraid that the Octopus does not support task log capturing thru a PowerShell script or any other programmatical means possible. Is there any way to capture these logs?

Thanks,

Hi Tarik

Thanks for getting in touch!

I would suggest to go with option 2.

Octopus does support capturing task log, you can use any of our supported languages to do that.

Firstly, go to Library , External Feeds and add a new feed for the public Nuget server, with a URL of https://api.nuget.org/v3/index.json . Feel free to enable the extended API.

After that, create a script step in your project, and in the Referenced Packages section add a reference to the Octopus.Client package in the new external feed you created.

Finally, in the Inline Source Code section, select C# and provide your script and put in the follow scripts. You will need to set the server address as well as the API key, you can find more information about how to generate API key from this link https://octopus.com/docs/api-and-integration/api/how-to-create-an-api-key

#r "Octopus.Client\lib\net45\Octopus.Client.dll"
  
using Octopus.Client;
  
var repository = new OctopusRepository(new OctopusServerEndpoint("YOUR_SERVER_ADDRESS", "YOUR_API_KEY"));
var task = repository.Tasks.Get(Octopus.Parameters["Octopus.Task.Id"]);
var taskLog = repository.Tasks.GetRawOutputLog(task);
Console.Write(taskLog);

I have tested this one locally and it works fine for me.

I hope this helps!

Let me know how you go

Regards
Eddy

Thanks! It seems to be working. I eventually went with the following code:

#r "Octopus.Client\lib\net45\Octopus.Client.dll"
#r Octopus.Parameters["Octopus.Action.Package[Microsoft.ApplicationInsights].ExtractedPath"]

using Microsoft.ApplicationInsights;
using Octopus.Client;

var repository = new OctopusRepository(new OctopusServerEndpoint("SERVER_HOST_URL", "API-KEY"));
var task = repository.Tasks.Get(Octopus.Parameters["Octopus.Task.Id"]);
var steps = repository.Tasks
.GetDetails(task)
  .ActivityLogs
  .First().Children.Where(a => a.Ended.HasValue).Select(a =>
							 new
							 {
								 a.Name,
								 Started = a.Started.Value,
								 Ended = a.Ended.Value,
								 Success = a.Status == ActivityStatus.Success,
								 Duration = TimeSpan.FromSeconds((a.Ended.Value - a.Started.Value).Seconds)
							 }).ToList();


var client = new TelemetryClient();
client.InstrumentationKey = YOUR_INSTRUMENTATION_KEY_FROM_AZURE";

foreach (var step in steps)
{
	client.TrackEvent(step.Name,
		new Dictionary<string, string> {
			{ "Name", step.Name },
			{ "Started", step.Started.ToString()},
			{ "Ended", step.Ended.ToString() },
			{ "Success", step.Success.ToString() }
		},
		new Dictionary<string, double>() {
		{"Duration", step.Duration.TotalSeconds},
	});
}

client.Flush();

Hi Tarik

I am glad to hear that. Feel free to contact us again if you have any other issues.

Regards
Eddy

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.