serilog-contrib / serilog-sinks-slack

A simple (yet customizable) Slack logging sink for Serilog
MIT License
41 stars 27 forks source link

Feature Request: Filtering Logs based on Properties #49

Open himanshusaini111 opened 3 months ago

himanshusaini111 commented 3 months ago

I have two Slack Sinks that log to different Channels.

One Channel is for logs and the other is for alerts. Like below:

.WriteTo.Slack( webhookUrl: "https://hooks.slack.com/services/hook1", //hook for alerting CST customChannel: "aletr", customUsername: $"alert-bot", customIcon: ":radioactive_sign:", restrictedToMinimumLevel: LogEventLevel.Fatal ) .WriteTo.Slack( webhookUrl: "https://hooks.slack.com/services/hook2", customChannel: "logs", customUsername: $"log-bot", customIcon: ":radioactive_sign:", restrictedToMinimumLevel: LogEventLevel.Error );

Currently I am managing this by making the alert Channels log level as Fatal. But I wan to log Certain Errors as Alerts.

But I cant do that directly as then I will have decrease the logLevel for Alert Channel and it will be polluted by all the Error Logs

TrapperHell commented 3 months ago

I am of the opinion that this is not the right way to achieve what you're looking for, and is beyond the scope and intents of this sink.

I believe that Serilog.Expressions might be what you're looking for in order to accomplish this.

Amongst other ways, I think you could use the EventId as a selector. Consider the following configuration:

"WriteTo": [
{
  "Name": "Logger",
  "Args": {
    "configureLogger": {
      "Filter": [
      {
        "Name": "ByIncludingOnly",
        "Args": {
          "expression": "EventId.Id = 1"
        }
      }],
      "WriteTo": [
      {
        "Name": "Slack",
        "Args": {
          "webHookUrl": "..."
        }
      }]
    }
  }
},
{
  "Name": "Logger",
  "Args": {
    "configureLogger": {
      "Filter": [
      {
        "Name": "ByIncludingOnly",
        "Args": {
          "expression": "EventId.Id = 2"
        }
      }],
      "WriteTo": [
      {
        "Name": "Slack",
        "Args": {
          "webHookUrl": "..."
        }
      }]
    }
  }
}]

It's a bit bulky and repetitive but it does get the job done. As for usage, it goes something like this:

logger.LogWarning(1, "...");