Applying Advanced Logging in a gRPC Application
Explore how to implement advanced logging in gRPC applications by configuring logging interceptors for both server and client. Understand how to capture calls and exceptions across different gRPC call types and register interceptors in ASP.NET Core projects to enhance monitoring.
We'll cover the following...
To see the logs produced by the framework, we need to configure the logging provider and logging level. There's also a way to apply custom logging to capture what the framework is doing. To do so, we can configure logging interceptors for both the client and server.
There are many types of scenarios where using logging interceptors would be needed. For example, certain systems accept logs in a specific proprietary format, which cannot be applied by using standard libraries. Also, we might want to apply additional custom metadata to our logs, which interceptors will allow us to achieve easily.
We briefly covered gRPC interceptors when we looked at the advanced gRPC configuration. In this lesson, we'll cover them in more detail and see how they can be used for logging. We will start with the following project setup.
{
"profiles": {
"BasicGrpcService": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://127.0.0.1:5100;https://127.0.0.1:7100",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
We'll first add an interceptor to the gRPC server application. Then, we'll do the same to the client application.
Adding a logging interceptor to the gRPC server
We'll open the BasicGrpcService project folder and add the LoggingInterceptor.cs file to it with the following content.
In ...