I'm using Serilog.Sinks.MSSqlServer package version 5.8.0
I'm using this configuration for create my custom columns
var dbColumnOptions = new ColumnOptions
{
AdditionalDataColumns = new Collection<DataColumn>
{
new DataColumn {DataType = typeof(string), ColumnName = "Controller"},
new DataColumn {DataType = typeof(string), ColumnName = "Route"},
new DataColumn {DataType = typeof(string), ColumnName = "StatusCode"},
new DataColumn {DataType = typeof(string), ColumnName = "Method"},
new DataColumn {DataType = typeof(string), ColumnName = "Request"},
new DataColumn {DataType = typeof(string), ColumnName = "Response"}
}
};
dbColumnOptions.Store.Remove(StandardColumn.Properties);
dbColumnOptions.Store.Remove(StandardColumn.MessageTemplate);
dbColumnOptions.Store.Remove(StandardColumn.LogEvent);
dbColumnOptions.TimeStamp.ConvertToUtc = true;
LoggerConfiguration loggerConfig = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.MSSqlServer(dbLogConnection, tableName, columnOptions: dbColumnOptions, schemaName: "any", autoCreateSqlTable: true);
And I'm using one DelegatingHandler for catch the information of the request and response to call one API, this is my code where it's writing to SQL using serilog.
var requestUri = string.Empty;
var httpVerb = string.Empty;
var httpStatusCode = string.Empty;
var requestBody = string.Empty;
string responseBody = string.Empty;`
requestUri = request.RequestUri.ToString();
httpVerb = request.Method.Method;
if (request.Content != null)
{
requestBody = await request.Content.ReadAsStringAsync();
}
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
httpStatusCode = ((int)response.StatusCode).ToString();
if (response.Content != null)
{
responseBody = await response.Content.ReadAsStringAsync();
}
_logger.ForContext("Controller", "Whatever")
.ForContext("Request", requestBody)
.ForContext("Route", requestUri)
.ForContext("StatusCode", httpStatusCode)
.ForContext("Method", httpVerb)
.ForContext("Response", responseBody)
.Information("Information");
But the information about request body or response body it's always truncated, even if the column has NVarchar(max) as type.
I don't know why is happening, if I print in the console the information about the response or request the information is not truncated, only when it wrote on the DB.