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

Feature Request: Filtering Logs based on Properties #49

Open
himanshusaini111 opened this issue May 17, 2024 · 1 comment
Open

Feature Request: Filtering Logs based on Properties #49

himanshusaini111 opened this issue May 17, 2024 · 1 comment
Assignees
Labels

Comments

@himanshusaini111
Copy link

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 TrapperHell self-assigned this May 17, 2024
@TrapperHell
Copy link
Member

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, "...");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants