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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With