简体   繁体   English

如何在.NET Core 3.0中启用gRPC的日志记录

[英]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. 我有一个ASP.NET Core 3.0(3.0.0-preview8-28405-07)服务器应用程序,它使用带有协议缓冲区的gRPC(0.1.22)。 I'd like to enable logging to a file or console. 我想启用日志记录到文件或控制台。

Below is an example of a .Startup file: 下面是.Startup文件的示例:

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: 和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. gRPC具有一个名为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 . 在ASP.NET Core 3中使用GRPC进行日志记录的方式与任何其他ASP.NET应用程序相同

You can enable logging by running 您可以通过运行来启用日志记录

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

in your program.cs entry point file or by running 在您的program.cs入口点文件中或通过运行

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

in your ConfigureServices method in startup.cs startup.cs中的ConfigureServices方法中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM