ASP. NET Core Extension Library Http Log Explanation

  • 2021-11-29 06:44:20
  • OfStack

Directory 1. Open server request log 2. Open client request log 3. Examples

Best practices tell us not to log requests in detail because of security issues, but in real-world development, request details are important for quickly locating problems and sometimes strong evidence of the system. The Xfrogcn. AspNetCore. Extensions extension library provides server-side and client-side detailed logging, which can be turned on by configuration.

Server-side logging is done by requesting middleware, which records the request and response details at Trace level and the request time at Debug level. The request log for the service is named ServerRequest. Logger

To open the server details log, you only need to set the ServerRequestLevel property in the extension library configuration to Verbose level, which defaults to Information, so the request details and request time consumption will not be recorded.

When request details are turned on, there is a performance impact due to the need to read the details of the request and reply. At the same time, since the request body is to be read, the buffer of the request will be automatically opened. Details are read only when detailed logs are required, so there is little impact on performance after shutdown.

The request detailed log of customer service is based on IHttpClientFactory and HttpClient framework, and a log recording pipeline is added to the client request pipeline processing. The request processing pipeline logs the request and reply details at the Trace level, and if an exception occurs to the request, the exception details are logged at the Error level. The client request log is named ClientRequest. Logger

To turn on client request verbose logging, simply set EnableClientRequestLog to true in the extension library configuration and set ClientRequestLevel to Verbose, which defaults to Information. Like Server 1, request and response details will only be recorded when the conditions are met, so if they are not turned on, there will be no impact on performance. Note that when EnableClientRequestLog is set to false, the extension library does not insert the log request pipeline into the client request pipeline. This setting defaults to true.

1. Open the server request log

To open request verbose logging on the server side, simply reference the Xfrogcn. AspNetCore. Extensions library, and then, in the Startup class, configure the service request level to Verbose:


    public void ConfigureServices(IServiceCollection services)
    {
        services.AddExtensions(Configuration, config=>
        {
            config.FileLog = true;
            config.ConsoleLog = true;
            //  Set the server request log level to Verbose
            config.ServerRequestLevel = Serilog.Events.LogEventLevel.Verbose;
        });
        services.AddControllers();
    }

2. Open client request log

To turn on client logging, simply reference the Xfrogcn. AspNetCore. Extensions library, and then in the Startup class, configure ClientRequestLevel to Verbose and EnableClientRequestLog to true.


    class Program
    {
        static async Task Main(string[] args)
        {
            IServiceCollection services = new ServiceCollection()
                .AddExtensions(null, config =>
                {
                    config.EnableClientRequestLog = true;
                    config.ClientRequestLevel = Serilog.Events.LogEventLevel.Verbose;
                    config.ConsoleLog = true;
                });

            IServiceProvider provider = services.BuildServiceProvider();
            var clientFactory = provider.GetRequiredService<IHttpClientFactory>();
            HttpClient client = clientFactory.CreateClient();

            var response = await client.GetAsync("http://localhost:5000/weatherforecast");

            Console.ReadLine();
        }
    }

3. Examples

Refer to GitHub for a detailed example

Xfrogcn. AspNetCore. Extensions Address: GitHub Gitee

The above is the ASP. NET Core Extension Library Http Log Details, more about ASP. NET Core Extension Library Http Log information please pay attention to other related articles on this site!


Related articles: