大型 http 有效负载不会发送到 .NET Core 3.1 中的 Serilog Http Sink 端点

Large http payloads aren't getting sent to the Serilog Http Sink endpoint in .NET Core 3.1

总结

当日志数据很大时,我无法从 Serilog(Http 接收器)发布到我的自定义 .NET Core 3.1 WebAPI 端点。如果我在进行日志记录时删除了一些日志数据,那么 Serilog 会正确地接收到我的 WebAPI 端点。

我的配置

            new LoggerConfiguration()
                     .Enrich.FromLogContext()
                     .WriteTo.Http(httpPath, httpClient: new CustomHttpClient(), batchPostingLimit: int.MaxValue, queueLimit: int.MaxValue)
                     .CreateLogger();

我的自定义 Http 客户端

public class CustomHttpClient : IHttpClient
{
    private readonly HttpClient c_httpClient;

    public CustomHttpClient()
    {
        c_httpClient = new HttpClient
        {
            MaxResponseContentBufferSize = 2147483647L
        };
    }

    public void Configure(IConfiguration configuration)
    {
        
    }

    public Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content) => c_httpClient.PostAsync(requestUri, content);

    public void Dispose() => c_httpClient?.Dispose();
}

记录的实际作用

            var exceptionModel = new AppMonModel
            {
                Application = "SerilogMvc Sample Application",
                Message = ex.Message,
                Source = "SerilogMvc.HomeController.Index",
                StackTrace = ex.StackTrace,
                InnerException = ex.InnerException?.StackTrace,
                Details = "Sample details here",
                InsertDate = DateTime.Now,
                Severity = 100,
                UserDescription = "Keyvan User",
                ScreenshotBase64String = Convert.ToBase64String(System.IO.File.ReadAllBytes("C:/SamplePath/Untitled.png"))
            };
            
            c_logger.LogError(ex, "{exceptionModel}", exceptionModel);

我的端点

    [HttpPost("log")]
    [DisableRequestSizeLimit]
    public void Log([FromBody] object logEvents) { ... }

串行日志错误

事件 JSON 表示超出了为此接收器设置的字节大小限制 262144,将被丢弃;

问题

当我从我的 exceptionModel 对象中删除 ScreenshotBase64String = Convert.ToBase64String(System.IO.File.ReadAllBytes("C:/SamplePath/Untitled.png")) 时,我在我的 WebAPI 端点中看到错误。一旦我重新添加它,它甚至没有到达终点。

如果您需要更多详细信息,请告诉我。我很乐意提供它们。

打开自记录后答案就很简单了。这是我需要进行的更改以增加批格式化程序的大小:

        var defaultBatchFormatter = new DefaultBatchFormatter(batchFormatterSize);

        Log.Logger = new LoggerConfiguration()
                     .MinimumLevel.Error()
                     .Enrich.FromLogContext()
                     .WriteTo.Http(httpPath, batchFormatter: defaultBatchFormatter)
                     .CreateLogger();

需要增加批格式化程序的大小。