Harmonickey / Serilog.Sinks.SendGridEmail

Sends the email notification through SendGrid
MIT License
2 stars 3 forks source link

Use with Serilog Configuration #3

Closed steveoh closed 2 years ago

steveoh commented 2 years ago

Is your feature request related to a problem? Please describe.

It doesn't seem like I can use this sink with Serilog.Settings.Configuration

Describe the solution you'd like

{
  "Name": "SendGridEmail",
  "Args": {
    "EmailSubject": "",
    "FromEmail": "",
    "ToEmail": "",
    "ApiKey": ""
  }
}
Harmonickey commented 2 years ago

Can you provide more information about what you've tried? It looks like the documentation is clear that the base element must be "Serilog" and you need to put an item into a child-key array "Using" called "Serilog.Sinks.SendGridEmail"

I don't believe there are any changes that I could make to get it to "work" with the Serilog.Settings.Configuration, supposedly accordingly to their library it should "just work" with any sink.

steveoh commented 2 years ago

How do you set a client as json configuration?

Harmonickey commented 2 years ago

On startup, you would read from the json config file instead of reading from an xml config or hardcoding. This is how some .NET Core readmes and tutorials are suggesting, but you can change it to your liking.

var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json")
        .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
        .Build();
steveoh commented 2 years ago

I'm familiar. Still. How would you create the required sendgrid client as configuration?

Harmonickey commented 2 years ago

If you look at the documentation there appears to be some samples of some pretty advanced features that the author is allowing you to pass as arguments, so maybe it would look something like this... You'll probably have to play around with the 'type' parameter to get it right and to get Serilog.Settings.Configuration to pass the api key parameter to your send grid client. I haven't tried it out myself.

https://github.com/serilog/serilog-settings-configuration/blob/dev/sample/Sample/appsettings.json

{
  "Serilog": {
    "Using":  [ "Serilog.Sinks.SendGridEmail" ],
    "WriteTo:SendGrid": {
      "Name": "Email",
      "Args": {
        "connectionInfo": {
          "EmailSubject": "poaiwjef",
          "FromEmail": "aaa@aaa.com",
          "ToEmail": "aaa@aaa.com",
      "SendGridClient": {
          "type": "SendGrid",
          "apiKey": "your send grid api key"
      }
      "FromName": "aaa"
        },
        "restrictedToMinimumLevel": "Error"
      }
    },
  }
}