Skip to content

Commit

Permalink
Merge pull request #16 from panuoksala/improvedLogging
Browse files Browse the repository at this point in the history
Improved logging with Serilog
  • Loading branch information
panuoksala authored Mar 23, 2022
2 parents 1c5c850 + 1fab029 commit 988d41e
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 170 deletions.
134 changes: 81 additions & 53 deletions AzureDevOpsRunnerAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using StreamDeckAzureDevOps.Models;
using Microsoft.Extensions.Logging;
using Serilog;
using StreamDeckAzureDevOps.Models;
using StreamDeckAzureDevOps.Services;
using StreamDeckLib;
using StreamDeckLib.Messages;
Expand All @@ -11,27 +13,39 @@ namespace StreamDeckAzureDevOps
[ActionUuid(Uuid = "net.oksala.azuredevops.runner")]
public class AzureDevOpsRunnerAction : BaseAction
{
private readonly AzureDevOpsService _service = new AzureDevOpsService();
private readonly AzureDevOpsService _service;

public async Task UpdateStatus(string context)
public AzureDevOpsRunnerAction()
{
await Manager.SetImageAsync(context, "images/Azure-DevOps-updating.png");
_service = new AzureDevOpsService(Logger);
}

string statusImage = (PipelineType)SettingsModel.PipelineType switch
public async Task UpdateStatus(string context)
{
try
{
PipelineType.Build => await _service.GetBuildStatusImage(SettingsModel),
PipelineType.Release => await _service.GetReleaseStatusImage(SettingsModel),
_ => throw new ArgumentOutOfRangeException($"Unsupported pipeline type {SettingsModel.PipelineType}."),
};

if (statusImage != null)
await Manager.SetImageAsync(context, "images/Azure-DevOps-updating.png");

string statusImage = (PipelineType)SettingsModel.PipelineType switch
{
PipelineType.Build => await _service.GetBuildStatusImage(SettingsModel),
PipelineType.Release => await _service.GetReleaseStatusImage(SettingsModel),
_ => throw new ArgumentOutOfRangeException($"Unsupported pipeline type {SettingsModel.PipelineType}."),
};

if (statusImage != null)
{
await Manager.SetImageAsync(context, statusImage);
}
}
catch (Exception ex)
{
await Manager.SetImageAsync(context, statusImage);
Logger.LogError(ex, "Failed to update status.");
}
}

public override async Task OnDidReceiveSettings(StreamDeckEventPayload args)
{
{
await base.OnDidReceiveSettings(args);
}

Expand All @@ -52,12 +66,19 @@ public override async Task OnLongPress(StreamDeckEventPayload args)

public override async Task OnError(StreamDeckEventPayload args, Exception ex)
{
SettingsModel.ErrorMessage = ex.Message;
try
{
SettingsModel.ErrorMessage = ex.Message;

await Manager.ShowAlertAsync(args.context);
await Manager.SetImageAsync(args.context, "images/Azure-DevOps-unknown.png");
await Manager.ShowAlertAsync(args.context);
await Manager.SetImageAsync(args.context, "images/Azure-DevOps-unknown.png");

await Manager.SetSettingsAsync(args.context, SettingsModel);
await Manager.SetSettingsAsync(args.context, SettingsModel);
}
catch (Exception handlingException)
{
Logger.LogError(handlingException, $"Failed to handle error: {ex.Message}");
}
}

public override bool IsSettingsValid()
Expand All @@ -69,44 +90,51 @@ public override bool IsSettingsValid()

private async Task ExecuteKeyPress(StreamDeckEventPayload args, KeyPressAction keyPressAction)
{
PipelineType pipelineType = (PipelineType)SettingsModel.PipelineType;
switch (keyPressAction)
try
{
case KeyPressAction.DoNothing:
// Do nothing :)
break;

case KeyPressAction.UpdateStatus:
await UpdateDisplay(args);
await Manager.ShowOkAsync(args.context);
break;

case KeyPressAction.Run:
await Manager.SetImageAsync(args.context, "images/Azure-DevOps-updating.png");

if (pipelineType == PipelineType.Build)
{
await _service.StartBuild(SettingsModel);
}
else
{
await _service.StartRelease(SettingsModel);
}

await Manager.ShowOkAsync(args.context);
if((StatusUpdateFrequency)SettingsModel.UpdateStatusEverySecond == StatusUpdateFrequency.Never)
{
await Manager.SetImageAsync(args.context, "images/Azure-DevOps-success.png");
}
else
{
await Manager.SetImageAsync(args.context, "images/Azure-DevOps-waiting.png");
}
break;
PipelineType pipelineType = (PipelineType)SettingsModel.PipelineType;
switch (keyPressAction)
{
case KeyPressAction.DoNothing:
// Do nothing :)
break;

case KeyPressAction.UpdateStatus:
await UpdateDisplay(args);
await Manager.ShowOkAsync(args.context);
break;

case KeyPressAction.Run:
await Manager.SetImageAsync(args.context, "images/Azure-DevOps-updating.png");

if (pipelineType == PipelineType.Build)
{
await _service.StartBuild(SettingsModel);
}
else
{
await _service.StartRelease(SettingsModel);
}

await Manager.ShowOkAsync(args.context);
if ((StatusUpdateFrequency)SettingsModel.UpdateStatusEverySecond == StatusUpdateFrequency.Never)
{
await Manager.SetImageAsync(args.context, "images/Azure-DevOps-success.png");
}
else
{
await Manager.SetImageAsync(args.context, "images/Azure-DevOps-waiting.png");
}
break;
}

SettingsModel.ErrorMessage = string.Empty;
await Manager.SetSettingsAsync(args.context, SettingsModel);
}
catch (Exception ex)
{
Logger.LogError(ex, "Failed to initiate action from keypress.");
}

SettingsModel.ErrorMessage = string.Empty;
await Manager.SetSettingsAsync(args.context, SettingsModel);
}
}
}
3 changes: 0 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ class Program

static async Task Main(string[] args)
{

using (var config = StreamDeckLib.Config.ConfigurationBuilder.BuildDefaultConfiguration(args))
{

await ConnectionManager.Initialize(args, config.LoggerFactory)
.RegisterAllActions(typeof(Program).Assembly)
.StartAsync();

}

}
Expand Down
Loading

0 comments on commit 988d41e

Please sign in to comment.