1

I have a ASP.NET core web API where I'm using Serilog to do the logging.

I want to log Info from Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware in the log-file, but in the database I only want to log Warning from Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware.

Any easy way to this?

My current Serilog config looks more or less like this, but it logs Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware to the database.

    "Serilog": {
        "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer" ],
        "MinimumLevel": {
            "Default": "Debug",
            "Override": {
                "Microsoft.AspNetCore": "Warning",
                "Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
            }
        },
        "WriteTo": [
            { "Name": "Console" },
            {
                "Name": "File",
                "Args": {
                    "path": "Logs/log.txt",
                    "rollingInterval": "Day"
                }
            },
            {
                "Name": "MSSqlServer",
                "Args": {
                    "connectionString": "....",
                    "restrictedToMinimumLevel": "Information"
                    // More configuration options
                }
            }
        ]
    }
4
  • "restrictedToMinimumLevel": "Warning" ? Are you saying you get no entries in the database or that you get too many? Commented Jun 7, 2024 at 13:18
  • To many. I want "Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware" in the log-file and NOT in the database. Right now it is written both places. Commented Jun 10, 2024 at 6:23
  • Set the minimum level for the database to Warning instead of Information. You should probably use the fluent configuration API until you get the behavior you want too, and then move those settings to JSON. C# gives you autocompletion and compiler checks. With JSON you'll only find out that something's wrong at runtime but not what's actually wrong. Commented Jun 10, 2024 at 6:35
  • If I change minimum level for the database to Warning it seems to apply to all logging to the database, not just what comes from "Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware". Commented Jun 10, 2024 at 12:25

1 Answer 1

1

I had similar requirement except I wanted to format Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware in JSON format.

I went with Serilog Expressions, to conditionally route log events to different Sinks with different formatter.

Now you can further extend this configuration to add additional if check on log level and route it Database vs File sinks. This avoid unnecessary duplicate logs in all sinks.

I have written full blog post on my requirement at https://ranbook.cloud/posts/serilog-http-payload-logging/. Hope this helps!!

Below is the Serilog configuration in appsettings.json. Here I was sending all log events to Console but with different formatter.

{
    "Serilog": {
        "MinimumLevel": "Information",
        "WriteTo": [
            {
                "Name": "Conditional",
                "Args": {
                    "expression": "SourceContext <> 'Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware'",
                    "configureSink": {
                        "Console": {
                            "Name": "Console",
                            "Args": {
                                "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
                            }
                        }
                    }
                }
            },
            {
                "Name": "Conditional",
                "Args": {
                    "expression": "SourceContext = 'Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware'",
                    "configureSink": {
                        "Console": {
                            "Name": "Console",
                            "Args": {
                                "formatter": {
                                    "type": "serilog_payload_demo.Configuration.PayloadLogFormatter, serilog-payload-demo"
                                }
                            }
                        }
                    }
                }
            }
        ]
    },
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft.AspNetCore": "Warning"
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.