Skip to content

Commit

Permalink
feat: Add LoggerFactory to ChannelFactory (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemaitre-aneo authored Jul 19, 2024
2 parents 9869f3a + 2d745da commit a610b84
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions packages/csharp/ArmoniK.Api.Client/Submitter/GrpcChannelFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2023. All rights reserved.
// Copyright (C) ANEO, 2021-2024. All rights reserved.
// W. Kirschenmann <[email protected]>
// J. Gurhem <[email protected]>
// D. Dubuc <[email protected]>
Expand Down Expand Up @@ -261,13 +261,15 @@ private static SslProtocols GetSslProtocols()
/// <param name="optionsGrpcClient">Options for the creation of the channel</param>
/// <param name="handlerType">Which HttpMessageHandler type to use</param>
/// <param name="logger">Optional logger</param>
/// <param name="loggerFactory">Optional loggerFactory</param>
/// <returns>
/// The initialized GrpcChannel
/// </returns>
/// <exception cref="InvalidOperationException">Endpoint passed through options is missing</exception>
private static GrpcChannel CreateChannelInternal(GrpcClient optionsGrpcClient,
HandlerType handlerType,
ILogger? logger)
private static GrpcChannel CreateChannelInternal(GrpcClient optionsGrpcClient,
HandlerType handlerType,
ILogger? logger,
ILoggerFactory? loggerFactory)
{
if (string.IsNullOrEmpty(optionsGrpcClient.Endpoint))
{
Expand Down Expand Up @@ -417,6 +419,7 @@ private static GrpcChannel CreateChannelInternal(GrpcClient optionsGrpcClient,
Credentials = credentials,
DisposeHttpClient = true,
ServiceConfig = serviceConfig,
LoggerFactory = loggerFactory,
};

if (handlerType is HandlerType.Web)
Expand All @@ -439,29 +442,35 @@ private static GrpcChannel CreateChannelInternal(GrpcClient optionsGrpcClient,
/// </summary>
/// <param name="optionsGrpcClient">Options for the creation of the channel</param>
/// <param name="logger">Optional logger</param>
/// <param name="loggerFactory">Optional loggerFactory</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>
/// The initialized GrpcChannel
/// </returns>
/// <exception cref="InvalidOperationException">Endpoint passed through options is missing</exception>
public static Task<GrpcChannel> CreateChannelAsync(GrpcClient optionsGrpcClient,
ILogger? logger = null,
ILoggerFactory? loggerFactory = null,
CancellationToken cancellationToken = default)
=> Task.FromResult(CreateChannel(optionsGrpcClient,
logger));
logger,
loggerFactory));

/// <summary>
/// Creates the GrpcChannel
/// </summary>
/// <param name="optionsGrpcClient">Options for the creation of the channel</param>
/// <param name="logger">Optional logger</param>
/// <param name="loggerFactory">Optional loggerFactory</param>
/// <returns>
/// The initialized GrpcChannel
/// </returns>
/// <exception cref="InvalidOperationException">Endpoint passed through options is missing</exception>
public static GrpcChannel CreateChannel(GrpcClient optionsGrpcClient,
ILogger? logger = null)
public static GrpcChannel CreateChannel(GrpcClient optionsGrpcClient,
ILogger? logger = null,
ILoggerFactory? loggerFactory = null)
{
logger ??= loggerFactory?.CreateLogger<GrpcChannel>();
if (!string.IsNullOrEmpty(optionsGrpcClient.OverrideTargetName))
{
logger?.LogWarning("OverrideTargetName is not supported");
Expand All @@ -472,15 +481,17 @@ public static GrpcChannel CreateChannel(GrpcClient optionsGrpcClient,
{
return CreateChannelInternal(optionsGrpcClient,
handlerType,
logger);
logger,
loggerFactory);
}

// If dotnet core (>= Net 5), we can use HttpClientHandler
if (!RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework"))
{
return CreateChannelInternal(optionsGrpcClient,
HandlerType.Http,
logger);
logger,
loggerFactory);
}

// If dotnet framework, we can use a plain WinHttpHandler.
Expand All @@ -489,7 +500,8 @@ public static GrpcChannel CreateChannel(GrpcClient optionsGrpcClient,
{
return CreateChannelInternal(optionsGrpcClient,
HandlerType.Win,
logger);
logger,
loggerFactory);
}
catch (InvalidOperationException e)
{
Expand All @@ -499,7 +511,8 @@ public static GrpcChannel CreateChannel(GrpcClient optionsGrpcClient,
"Falling back to gRPC Web that does not fully support gRPC streams");
return CreateChannelInternal(optionsGrpcClient,
HandlerType.Web,
logger);
logger,
loggerFactory);
}
}

Expand Down

0 comments on commit a610b84

Please sign in to comment.