diff --git a/Conductor/Client/Authentication/TokenHandler.cs b/Conductor/Client/Authentication/TokenHandler.cs index 01ff275d..329a65b1 100644 --- a/Conductor/Client/Authentication/TokenHandler.cs +++ b/Conductor/Client/Authentication/TokenHandler.cs @@ -1,5 +1,7 @@ +using conductor.csharp.Client.Extensions; using Conductor.Api; using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Logging; using System; namespace Conductor.Client.Authentication @@ -9,10 +11,12 @@ public class TokenHandler private static int REFRESH_TOKEN_RETRY_COUNTER_LIMIT = 5; private static readonly object _lockObject = new object(); private readonly MemoryCache _memoryCache; + private static ILogger _logger; public TokenHandler() { _memoryCache = new MemoryCache(new MemoryCacheOptions()); + _logger = ApplicationLogging.CreateLogger(); } public string GetToken(OrkesAuthenticationSettings authenticationSettings, TokenResourceApi tokenClient) @@ -50,7 +54,7 @@ private string GetTokenFromServer(OrkesAuthenticationSettings authenticationSett } catch (Exception e) { - Console.WriteLine($"Failed to refresh authentication token, attempt = {attempt}, error = {e.Message}"); + _logger.LogError($"Failed to refresh authentication token, attempt = {attempt}, error = {e.Message}"); } } throw new Exception("Failed to refresh authentication token"); diff --git a/Conductor/Client/Extensions/ApplicationLogging.cs b/Conductor/Client/Extensions/ApplicationLogging.cs new file mode 100644 index 00000000..19040a38 --- /dev/null +++ b/Conductor/Client/Extensions/ApplicationLogging.cs @@ -0,0 +1,11 @@ +using Microsoft.Extensions.Logging; + +namespace conductor.csharp.Client.Extensions +{ + public static class ApplicationLogging + { + public static ILoggerFactory LoggerFactory = new LoggerFactory(); + public static ILogger CreateLogger() => LoggerFactory.CreateLogger(); + public static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName); + } +} diff --git a/Conductor/Client/Extensions/WorkflowExtensions.cs b/Conductor/Client/Extensions/WorkflowExtensions.cs index e9f3b708..1f8dc664 100644 --- a/Conductor/Client/Extensions/WorkflowExtensions.cs +++ b/Conductor/Client/Extensions/WorkflowExtensions.cs @@ -3,12 +3,15 @@ using System.Collections.Generic; using System; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using conductor.csharp.Client.Extensions; namespace Conductor.Client.Extensions { public class WorkflowExtensions { private static int RETRY_ATTEMPT_LIMIT = 3; + private static ILogger _logger = ApplicationLogging.CreateLogger(); public static async Task> StartWorkflows(WorkflowResourceApi workflowClient, Models.StartWorkflowRequest startWorkflowRequest, int maxAllowedInParallel, int total) { @@ -18,7 +21,7 @@ public static async Task> StartWorkflows(WorkflowResourceA { await StartWorkflowBatch(workflowClient, startWorkflowRequest, maxAllowedInParallel, workflowIds); } - Console.WriteLine($"Started {workflowIds.Count} workflows"); + _logger.LogInformation($"Started {workflowIds.Count} workflows"); return workflowIds; } @@ -29,7 +32,7 @@ public static async Task> StartWorkflows(WorkflowResourceA { await GetWorkflowStatusBatch(workflowClient, workflowStatusList, index, index + maxAllowedInParallel, workflowIds); } - Console.WriteLine($"Got ${workflowStatusList.Count} workflow statuses"); + _logger.LogInformation($"Got ${workflowStatusList.Count} workflow statuses"); return workflowStatusList; } @@ -65,7 +68,7 @@ private static void GetWorkflowStatus(WorkflowResourceApi workflowClient, Concur } catch (ApiException e) { - Console.WriteLine($"Failed to get workflow status, reason: {e}"); + _logger.LogError($"Failed to get workflow status, reason: {e}"); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1 << attempt)); } } @@ -82,7 +85,7 @@ private static void StartWorkflow(WorkflowResourceApi workflowClient, Models.Sta } catch (ApiException e) { - Console.WriteLine($"Failed to start workflow, reason: {e}"); + _logger.LogError($"Failed to start workflow, reason: {e}"); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1 << attempt)); } } diff --git a/Tests/Worker/WorkerTests.cs b/Tests/Worker/WorkerTests.cs index e67546b5..d1f77312 100644 --- a/Tests/Worker/WorkerTests.cs +++ b/Tests/Worker/WorkerTests.cs @@ -1,8 +1,10 @@ +using conductor.csharp.Client.Extensions; using Conductor.Api; using Conductor.Client.Extensions; using Conductor.Client.Models; using Conductor.Definition; using Conductor.Definition.TaskType; +using Microsoft.Extensions.Logging; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -19,10 +21,12 @@ public class WorkerTests private const string TASK_DOMAIN = "taskDomain"; private readonly WorkflowResourceApi _workflowClient; + private readonly ILogger _logger; public WorkerTests() { _workflowClient = ApiExtensions.GetClient(); + _logger = ApplicationLogging.CreateLogger(); } [Fact] @@ -77,7 +81,7 @@ private async System.Threading.Tasks.Task ValidateWorkflowCompletion(params stri if (workflowStatus.Status.Value != WorkflowStatus.StatusEnum.COMPLETED) { incompleteWorkflowCounter += 1; - Console.WriteLine($"Workflow not completed, workflowId: {workflowStatus.WorkflowId}"); + _logger.LogInformation($"Workflow not completed, workflowId: {workflowStatus.WorkflowId}"); } } Assert.Equal(0, incompleteWorkflowCounter); diff --git a/csharp-examples/Runner.cs b/csharp-examples/Runner.cs index eec36c58..02134d01 100644 --- a/csharp-examples/Runner.cs +++ b/csharp-examples/Runner.cs @@ -1,4 +1,5 @@ using System.Collections; +using conductor.csharp.Client.Extensions; using Conductor.Client; using Conductor.Client.Authentication; using Conductor.Client.Extensions; @@ -9,37 +10,53 @@ namespace csharp_examples; public class Runner { + public readonly ILogger _logger; + + public Runner() + { + _logger = ApplicationLogging.CreateLogger(); + } + /// /// Running multiple task as background services /// public async void StartTasks() { - var key = Environment.GetEnvironmentVariable("KEY"); - var secret = Environment.GetEnvironmentVariable("SECRET"); - var url = Environment.GetEnvironmentVariable("CONDUCTOR_SERVER_URL"); - - var configuration = new Configuration - { - BasePath = url, - AuthenticationSettings = new OrkesAuthenticationSettings(key, secret) - }; - var num = 5; - for (var i = 1; i <= num; i++) + try { - var host = WorkflowTaskHost.CreateWorkerHost(configuration, LogLevel.Information, - new TestWorker("csharp_task_" + i)); - var ct = new CancellationTokenSource(); - try + var key = Environment.GetEnvironmentVariable("KEY"); + var secret = Environment.GetEnvironmentVariable("SECRET"); + var url = Environment.GetEnvironmentVariable("CONDUCTOR_SERVER_URL"); + + var configuration = new Configuration { - await host.StartAsync(ct.Token); - } - catch (Exception e) + BasePath = url, + AuthenticationSettings = new OrkesAuthenticationSettings(key, secret) + }; + var num = 5; + for (var i = 1; i <= num; i++) { - Console.WriteLine(e); - throw; + var host = WorkflowTaskHost.CreateWorkerHost(configuration, LogLevel.Information, + new TestWorker("csharp_task_" + i)); + var ct = new CancellationTokenSource(); + try + { + await host.StartAsync(ct.Token); + } + catch (Exception e) + { + _logger.LogError(e.Message); + throw; + } } - } - while (true) Thread.Sleep(TimeSpan.FromDays(1)); + while (true) + Thread.Sleep(TimeSpan.FromDays(1)); // after 1 year will stop the service + + } + catch (Exception e) + { + _logger.LogError($"{e.Message}"); + } } } \ No newline at end of file