Skip to content

Commit

Permalink
Update logging to use generated logs (#2531)
Browse files Browse the repository at this point in the history
  • Loading branch information
wabalubdub authored Sep 12, 2024
1 parent 3ed9fb9 commit de9a82f
Show file tree
Hide file tree
Showing 23 changed files with 384 additions and 1,088 deletions.
21 changes: 6 additions & 15 deletions src/Grpc.AspNetCore.HealthChecks/GrpcHealthChecksPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

namespace Grpc.AspNetCore.HealthChecks;

internal sealed class GrpcHealthChecksPublisher : IHealthCheckPublisher
internal sealed partial class GrpcHealthChecksPublisher : IHealthCheckPublisher
{
private readonly HealthServiceImpl _healthService;
private readonly ILogger _logger;
Expand Down Expand Up @@ -75,22 +75,13 @@ public Task PublishAsync(HealthReport report, CancellationToken cancellationToke
return Task.CompletedTask;
}

private static class Log
private static partial class Log
{
private static readonly Action<ILogger, int, int, Exception?> _evaluatingPublishedHealthReport =
LoggerMessage.Define<int, int>(LogLevel.Trace, new EventId(1, "EvaluatingPublishedHealthReport"), "Evaluating {HealthReportEntryCount} published health report entries against {ServiceMappingCount} service mappings.");
[LoggerMessage(Level = LogLevel.Trace, EventId = 1, EventName = "EvaluatingPublishedHealthReport", Message = "Evaluating {HealthReportEntryCount} published health report entries against {ServiceMappingCount} service mappings.")]
public static partial void EvaluatingPublishedHealthReport(ILogger logger, int healthReportEntryCount, int serviceMappingCount);

private static readonly Action<ILogger, string, HealthCheckResponse.Types.ServingStatus, int, Exception?> _serviceMappingStatusUpdated =
LoggerMessage.Define<string, HealthCheckResponse.Types.ServingStatus, int>(LogLevel.Debug, new EventId(2, "ServiceMappingStatusUpdated"), "Service '{ServiceName}' status updated to {Status}. {EntriesCount} health report entries evaluated.");
[LoggerMessage(Level = LogLevel.Debug, EventId = 2, EventName = "ServiceMappingStatusUpdated", Message = "Service '{ServiceName}' status updated to {Status}. {EntriesCount} health report entries evaluated.")]
public static partial void ServiceMappingStatusUpdated(ILogger logger, string serviceName, HealthCheckResponse.Types.ServingStatus status, int entriesCount);

public static void EvaluatingPublishedHealthReport(ILogger logger, int healthReportEntryCount, int serviceMappingCount)
{
_evaluatingPublishedHealthReport(logger, healthReportEntryCount, serviceMappingCount, null);
}

public static void ServiceMappingStatusUpdated(ILogger logger, string serviceName, HealthCheckResponse.Types.ServingStatus status, int entriesCount)
{
_serviceMappingStatusUpdated(logger, serviceName, status, entriesCount, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Grpc.AspNetCore.ClientFactory;
/// The interceptor gets the request from IHttpContextAccessor, which is a singleton.
/// IHttpContextAccessor uses an async local value.
/// </summary>
internal class ContextPropagationInterceptor : Interceptor
internal partial class ContextPropagationInterceptor : Interceptor
{
private readonly GrpcContextPropagationOptions _options;
private readonly IHttpContextAccessor _httpContextAccessor;
Expand Down Expand Up @@ -258,15 +258,10 @@ public async Task<bool> MoveNext(CancellationToken cancellationToken)
}
}

private static class Log
private static partial class Log
{
private static readonly Action<ILogger, string, Exception?> _propagateServerCallContextFailure =
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, "PropagateServerCallContextFailure"), "Unable to propagate server context values to the call. {ErrorMessage}");

public static void PropagateServerCallContextFailure(ILogger logger, string errorMessage)
{
_propagateServerCallContextFailure(logger, errorMessage, null);
}
[LoggerMessage(Level = LogLevel.Debug, EventId = 1, EventName = "PropagateServerCallContextFailure", Message = "Unable to propagate server context values to the call. {ErrorMessage}")]
public static partial void PropagateServerCallContextFailure(ILogger logger, string errorMessage);
}

// Store static callbacks so delegates are allocated once
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Extension methods for the gRPC reflection services.
/// </summary>
public static class GrpcReflectionServiceExtensions
public static partial class GrpcReflectionServiceExtensions
{
/// <summary>
/// Adds gRPC reflection services to the specified <see cref="IServiceCollection" />.
Expand Down Expand Up @@ -150,14 +150,13 @@ public static IServiceCollection AddGrpcReflection(this IServiceCollection servi
return baseType;
}

private static class Log
private static partial class Log
{
private static readonly Action<ILogger, string, Exception?> _serviceDescriptorNotResolved =
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, "ServiceDescriptorNotResolved"), "Could not resolve service descriptor for '{ServiceType}'. The service metadata will not be exposed by the reflection service.");

[LoggerMessage(Level = LogLevel.Debug, EventId = 1, EventName = "ServiceDescriptorNotResolved", Message = "Could not resolve service descriptor for '{ServiceType}'. The service metadata will not be exposed by the reflection service.")]
private static partial void ServiceDescriptorNotResolved(ILogger logger, string serviceType);
public static void ServiceDescriptorNotResolved(ILogger logger, Type serviceType)
{
_serviceDescriptorNotResolved(logger, serviceType.FullName ?? string.Empty, null);
ServiceDescriptorNotResolved(logger, serviceType.FullName ?? string.Empty);
}
}
}
Loading

0 comments on commit de9a82f

Please sign in to comment.