Skip to content

How to store different types of logs in different files #435

@SunnyVachhetA

Description

@SunnyVachhetA
.NET 8
<PackageVersion Include="Serilog" Version="4.0.1" />
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.2" />
<PackageVersion Include="Serilog.Expressions" Version="5.0.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.File"
    ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft.AspNetCore.Mvc": "Warning",
        "Microsoft.AspNetCore.Routing": "Warning",
        "Microsoft.AspNetCore.Hosting": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "File",
        "Args": {
          "path": "Logs/log-.txt",
          "rollingInterval": "Day",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
          "fileSizeLimitBytes": 10485760,
          "rollOnFileSizeLimit": true,
          "shared": true,
          "flushToDiskInterval": 1
        },
        "Filter": [
          {
            "Name": "ByIncluding",
            "Args": {
              "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore.Database')"
            }
          }
        ]
      }
    ],
    "Enrich": [
      "FromLogContext"
    ]
  },
  "AllowedHosts": "*"
}
//Program.cs file
builder.Host.UseSerilog((context, config) => config.ReadFrom.Configuration(context.Configuration));

I want to store EF Core related query only in Log file, I will have another file for other type of logging.

I tried using Expression but didn't worked. What I am doing wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions