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

[.Net Core application] How to set BatchSizeLimit in appsettings.json #126

Open
dailyenergy opened this issue Feb 26, 2024 · 2 comments
Open

Comments

@dailyenergy
Copy link

by browsing through source code and other issues I have figured out that I can add Serilog Mail sink in appsettings as follows:

        "Name": "Email",
        "Args": {
          "From": "[email protected]",
          "To": "[email protected]",
          "Host": "localhost",
          "Port": 25,
          "Subject": "[{Level}] Log Email",
          "Body": "{Timestamp:yyyy-MM-dd HH:mm} [{Level}] <{MachineName}> {Message}{NewLine}{Exception}",
          "RestrictedToMinimumLevel": "Warning",

But I have no clue how to set BatchSizeLimit. Do I really need to create a custom extension for LoggerSinkConfiguration?

@MrPsi
Copy link

MrPsi commented Apr 4, 2024

Try this, also see #130

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information"
    },
    "WriteTo": [
      {
        "Name": "Email",
        "Args": {
          "options": {
            "subject": "Serilog test",
            "from": "[email protected]",
            "to": [ "[email protected]" ],
            "host": "localhost",
            "body": "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message}{NewLine}{Exception}{NewLine}"
          },
          "batchingOptions": {
            "batchSizeLimit": 10,
            "period": "00:00:01"
          },
          "restrictedToMinimumLevel": "Information"
        }
      }
    ]
  }
}

@bfnoling
Copy link

Thanks for your help @MrPsi. I have pretty much adapted your config in my appsettings.json but I am getting one email per logged message. The batching is not working.

I get batching to work when configuring a logger in the app,

await using var log = new LoggerConfiguration()
    .WriteTo.Email(
        options: new()
        {
            From = "[email protected]",
            To = new List<string> { "[email protected]" },
            Host = "localhost",
        },
        batchingOptions: new()
        {
            BatchSizeLimit = 10,
            BufferingTimeLimit = TimeSpan.FromSeconds(30),
        })
    .CreateLogger();

log.Information("Hello from Serilog!");
log.Information("Hello again from Serilog!");

but not from appsettings.json.

Any clues as to what I'm missing?

  "Serilog": {
    "Using": [ "Serilog.Sinks.Email", "Serilog.Sinks.Console", "App", "Serilog.Sinks.File" ],
    "MinimumLevel": "Information",
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "C:/bin/Apps/Logs/log.txt",
          "rollingInterval": "Day",
          "retainedFileCountLimit": "45"
        }
      },
      {
        "Name": "Email",
        "Args": {
          "options": {
            "subject": "App Error(s)",
            "from": "[email protected]",
            "to": [ "[email protected]" ],
            "host": "localhost",
            "body": "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message}{NewLine}{Exception}{NewLine}"
          },
          "batchingOptions": {
            "batchSizeLimit": 10,
            "period": "00:00:01"
          },
          "restrictedToMinimumLevel": "Error"
        }
      }
    ]
  }

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

No branches or pull requests

3 participants