serilog / serilog-aspnetcore

Serilog integration for ASP.NET Core
Apache License 2.0
1.29k stars 203 forks source link

Remove a property after message render #335

Open StrangeW opened 1 year ago

StrangeW commented 1 year ago

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 commented 1 year ago

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}".

nblumhardt commented 1 year ago

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!