大型 http 有效负载不会发送到 .NET Core 3.1 中的 Serilog Http 接收器终结点



摘要

当日志数据很大时,我在从Serilog(Http Sink(发布到我的自定义.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();

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

需要添加eventBodyLimitBytes

在此处输入图像描述

.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl, eventBodyLimitBytes: 1048576)

最新更新