Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ByExcluding filtering saved in appsettings.json #125

Open
aerott opened this issue Oct 22, 2024 · 6 comments
Open

ByExcluding filtering saved in appsettings.json #125

aerott opened this issue Oct 22, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@aerott
Copy link

aerott commented Oct 22, 2024

Hello community,
I'm not sure if this is a bug, but I can't figure out why it's not working.

I'm trying to create a PoC where NotImplementedException is filtered out and not logged.

When I apply the ByExcluding filter directly in code, it works as expected. However, it doesn't work when the settings are loaded from appsettings.json.

Reproduction
Here is sample of Asp.NET Web API project:

//appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Sentry.Serilog", "Serilog.Expressions" ],
    "MinimumLevel": {
      "Default": "Information"
    },
    "WriteTo": [
      {
        "Name": "Sentry",
        "Args": {
          "Dsn": "<dsn>"
        },
        "Filter": [
          {
            "Name": "ByExcluding",
            "Args": {
              "expression": "@exception is NotImplementedException" // tried many different variations
              // "@exception.GetType().Name != 'NotImplementedException'"
              // "Exception != null and Exception.GetType().FullName == 'System.NotImplementedException'"
              // "Exception is not null and Exception is System.NotImplementedException"
              // "Exception != null and Exception.GetType().Name == 'NotImplementedException'"
              // others...
            }
          }
        ]
      }
    ]
  }
}

using Serilog;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseSerilog((context, configuration) =>
{
    configuration.ReadFrom.Configuration(context.Configuration);

    // Filter out NotImplementedException -- works only with this
    configuration.Filter.ByExcluding(logEvent =>
        logEvent.Exception is NotImplementedException);
});

//Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Throwing exception:

throw new NotImplementedException();

The question is - how to exclude a specific exception in appsettings?

@aerott aerott added the bug Something isn't working label Oct 22, 2024
@bartelink
Copy link
Member

bartelink commented Oct 22, 2024

For usage questions like this, best to log them on stackoverflow, post a link here and close - this tracker is for bugs with repros and/or detailed feature requests.
There are very few watchers on the repos, and many users out on SO watching the serilog tag (including probably 90% of the watchers on repos), so if you're going to get an answer you're more likely to get it there first.

@aerott
Copy link
Author

aerott commented Oct 23, 2024

Hello,

I have discovered new details about my case.

I finally found the correct expression that filters the exception. It is also crucial where the "filter" section is located—it must be applied globally for it to work.

With the following settings, everything works fine.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Sentry.Serilog", "Serilog.Expressions" ],
    "Filter": [
      {
        "Name": "ByExcluding",
        "Args": {
          "expression": "TypeOf(@x) = 'System.NotImplementedException'"
        }
      }
    ],
    "MinimumLevel": {
      "Default": "Information"
    },
    "WriteTo": [
      {
        "Name": "Sentry",
        "Args": {
          "Dsn": "<dsn>"
        }
      }
    ]
  }
}

However, I wanted to filter only the Sentry sink, so I added the filter section as shown below.
Unfortunately, this doesn't work.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Sentry.Serilog", "Serilog.Expressions" ],
    "MinimumLevel": {
      "Default": "Information"
    },
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "File",
        "Args": {
          "path": "logs/log-.txt",
          "rollingInterval": "Day"
        }
      },
      {
        "Name": "Sentry",
        "Args": {
          "Dsn": "<dsn>"
        },
        "Filter": [
          {
            "Name": "ByExcluding",
            "Args": {
              "expression": "TypeOf(@x) = 'System.NotImplementedException'"
            }
          }
        ]
      }
    ]
  }
}

Is it possible to use the Filter section at the specific sink and saved this in appsettings.json?

@bartelink
Copy link
Member

My answer remains unchanged, post your full question on SO and post a link here
Anyone here that knows the answer can answer over there
But hopefully someone will answer there even before that

@AlaskanDruid
Copy link

AlaskanDruid commented Nov 3, 2024

May I ask which version of serilog are you using? Newest version seems to ignore "Filter". I may have to downgrade. I am running across the same (bug) as well. Filter must be at global level, not sink level, which is weird at best.

@stu640978
Copy link

Same Issue here.
Filter settings are working at global level but seems not working at sink level. I'm currently using Serilog 8.0.3 with Serilog.Expressions 5.0.0

@nblumhardt
Copy link
Member

Filtering at the sink level is implemented via WriteTo.Conditional(); I'm not sure what the JSON configuration for that looks like, but if you have the option to do this in C# it's a nicer experience all-round.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants