Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable logging for gRPC in .NET Core 3.0

I have an ASP.NET Core 3.0 (3.0.0-preview8-28405-07) server application which is using gRPC (0.1.22) with protocol buffers. I'd like to enable logging to a file or console.

Below is an example of a .Startup file:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddGrpc(options =>
        {
            options.EnableDetailedErrors = true;
        });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGrpcService<GreeterService>();
        });
    }
}

And the GreeterService:

public class GreeterService : Greeter.GreeterBase
{
    public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
    {
        return Task.FromResult(new HelloReply
        {
            Message = "Hello " + request.Name
        });
    }
}

gRPC has a logging class called Grpc.Core.Logging.ILogger. How can I set this up so it is logging to a file or console?

like image 579
Jordy Avatar asked Oct 28 '25 16:10

Jordy


1 Answers

Logging with GRPC in ASP.NET core 3 is handled the same way for any other ASP.NET app.

You can enable logging by running

hostBuilder.ConfigureLogging(logging =>
        {
            logging.AddConsole();
        })

in your program.cs entry point file or by running

serviceCollection.AddLogging(logging =>
            {
                logging.AddConsole();
            });

in your ConfigureServices method in startup.cs

like image 135
csteegz Avatar answered Oct 31 '25 06:10

csteegz