serilog-aspnetcore icon indicating copy to clipboard operation
serilog-aspnetcore copied to clipboard

Remove a property after message render

Open StrangeW opened this issue 2 years ago • 2 comments

Hello

Is your feature request related to a problem? Please describe. I'm not sure if this is a Feature, but I'll need to be able to remove a property from the logs once the message is build.

Describe the solution you'd like In idea, when I run

_logger.LogInformation("I run {ControlerName} at {RunDate}", nameof(HomeController), DateTime.Now);

I would like the message to be I run HomeController at 06/27/2023 17:45:23 but the RunDate property not to be present

{
	"@timestamp": "2023-06-27T17:45:23.0784393+02:00",
	"level": "Information",
	"messageTemplate": "I run {ControlerName} at {RunDate}",
	"message": "I run HomeController at 06/27/2023 17:45:23",
	"fields": {
		"ControlerName": "HomeController",
		"SourceContext": "MyApp.WebApp.Controllers.HomeController",
		"RequestPath": "/",
		"Environment": "DEV",
		"MachineName": "Test",
		"ApplicationName": "MyApp"
	}
}

Additional context I send my logs to applications like ElasticSearch or AppInsight and each property is indexed. I'd like to do away with these fields.

StrangeW avatar Jun 27 '23 16:06 StrangeW

I've already looked at ILogEventEnricher, where I can delete the property, but the message is interpreted afterwards. I get a message like "I run HomeController at {RunDate}" instead of "I run {ControlerName} at {RunDate}".

StrangeW avatar Jun 28 '23 07:06 StrangeW

Hi @StrangeW! A wrapper sink could achieve this (construct a new log event with new message template and selected properties, before forwarding to wrapped sink). LoggerSinkConfiguration.Wrap() is the starting point to check out. HTH!

nblumhardt avatar Jun 29 '23 23:06 nblumhardt