I only want ActionName logged once, in the request logging middleware. Not in 10 other log messages that are generated if I turn on tracing for SQL commands.
I found a good blog post on how to add it to the request logging middleware.
But I couldn't find how to remove it from all the other messages, so I wanted to share this solution. Not sure if it is the ideal solution, but it does work, in .Net 8 final. Feel free to add it to the docs, or just leave this ticket for web searchers to find.
Ideally asp.net would just stop injecting these.
// Remove ASP injected log properties that are either completely useless,
// or should only be logged once, in the request completion message.
//
// Usage: .Enrich.With(new RemoveMVCPropertiesEnricher())
//
public class RemoveMVCPropertiesEnricher : ILogEventEnricher
{
public void Enrich(LogEvent le, ILogEventPropertyFactory lepf)
{
string? sourceContext = GetPropertyStringValue(le, "SourceContext");
if (sourceContext != "Serilog.AspNetCore.RequestLoggingMiddleware") {
le.RemovePropertyIfPresent("ActionId");
le.RemovePropertyIfPresent("ActionName");
le.RemovePropertyIfPresent("RequestPath");
}
le.RemovePropertyIfPresent("ConnectionId"); // Is already part of RequestId
}
private string? GetPropertyStringValue(LogEvent le, string propertyName) {
object? val = GetPropertyScalarValue(le, propertyName);
if (val is string s) {
return s;
}
return null;
}
private object? GetPropertyScalarValue(LogEvent le, string propertyName) {
LogEventPropertyValue? val;
if (le.Properties.TryGetValue(propertyName, out val)) {
if (val is ScalarValue scalar) {
return scalar.Value;
}
}
return null;
}
}
I only want ActionName logged once, in the request logging middleware. Not in 10 other log messages that are generated if I turn on tracing for SQL commands.
I found a good blog post on how to add it to the request logging middleware.
But I couldn't find how to remove it from all the other messages, so I wanted to share this solution. Not sure if it is the ideal solution, but it does work, in .Net 8 final. Feel free to add it to the docs, or just leave this ticket for web searchers to find.
Ideally asp.net would just stop injecting these.