5.0 KiB
title | author | description | monikerRange | ms.author | ms.date | uid |
---|---|---|---|---|---|---|
Logging and diagnostics in gRPC on .NET | jamesnk | Learn how to gather diagnostics from your gRPC app on .NET. | >= aspnetcore-3.0 | jamesnk | 09/23/2019 | grpc/diagnostics |
Logging and diagnostics in gRPC on .NET
This article provides guidance for gathering diagnostics from your gRPC app to help troubleshoot issues.
gRPC services logging
[!WARNING] Server-side logs may contain sensitive information from your app. Never post raw logs from production apps to public forums like GitHub.
Since gRPC services are hosted on ASP.NET Core, it uses the ASP.NET Core logging system. In the default configuration, gRPC logs very little information, but this can configured. See the documentation on ASP.NET Core logging for details on configuring ASP.NET Core logging.
gRPC adds logs under the Grpc
category. To enable detailed logs from gRPC, configure the Grpc
prefixes to the Debug
level in your appsettings.json file by adding the following items to the LogLevel
sub-section in Logging
:
You can also configure this in Startup.cs with ConfigureLogging
:
If you aren't using JSON-based configuration, set the following configuration value in your configuration system:
Logging:LogLevel:Grpc
=Debug
Check the documentation for your configuration system to determine how to specify nested configuration values. For example, when using environment variables, two _
characters are used instead of the :
(for example, Logging__LogLevel__Grpc
).
We recommend using the Debug
level when gathering more detailed diagnostics for your app. The Trace
level produces very low-level diagnostics and is rarely needed to diagnose issues in your app.
Sample logging output
Here is an example of console output at the Debug
level of a gRPC service:
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/2 POST https://localhost:5001/Greet.Greeter/SayHello application/grpc
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint 'gRPC - /Greet.Greeter/SayHello'
dbug: Grpc.AspNetCore.Server.ServerCallHandler[1]
Reading message.
info: GrpcService.GreeterService[0]
Hello World
dbug: Grpc.AspNetCore.Server.ServerCallHandler[6]
Sending message.
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint 'gRPC - /Greet.Greeter/SayHello'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 1.4113ms 200 application/grpc
Access server-side logs
How you access server-side logs depends on the environment in which you're running.
As a console app
If you're running in a console app, the Console logger should be enabled by default. gRPC logs will appear in the console.
Other environments
If the app is deployed to another environment (for example, Docker, Kubernetes, or Windows Service), see xref:fundamentals/logging/index for more information on how to configure logging providers suitable for the environment.
gRPC client logging
[!WARNING] Client-side logs may contain sensitive information from your app. Never post raw logs from production apps to public forums like GitHub.
To get logs from the .NET client, you can set the GrpcChannelOptions.LoggerFactory
property when the client's channel is created. If you are calling a gRPC service from an ASP.NET Core app then the logger factory can be resolved from dependency injection (DI):
An alternative way to enable client logging is to use the gRPC client factory to create the client. A gRPC client registered with the client factory and resolved from DI will automatically use the app's configured logging.
If your app isn't using DI then you can create a new ILoggerFactory
instance with LoggerFactory.Create. To access this method add the Microsoft.Extensions.Logging package to your app.
Sample logging output
Here is an example of console output at the Debug
level of a gRPC client:
dbug: Grpc.Net.Client.Internal.GrpcCall[1]
Starting gRPC call. Method type: 'Unary', URI: 'https://localhost:5001/Greet.Greeter/SayHello'.
dbug: Grpc.Net.Client.Internal.GrpcCall[6]
Sending message.
dbug: Grpc.Net.Client.Internal.GrpcCall[1]
Reading message.
dbug: Grpc.Net.Client.Internal.GrpcCall[4]
Finished gRPC call.