serilog / serilog-sinks-email

A Serilog sink that writes events to SMTP email
Apache License 2.0
73 stars 68 forks source link

NetworkCredential and Port not being parsed from configuration file #58

Closed qwertyuiope closed 9 months ago

qwertyuiope commented 6 years ago

I am not being able to configure the network credentials and the port from the appsetting.json file. Follow the serilog section on my appsettings:

"Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "System": "Information",
        "Microsoft": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "Email",
        "Args": {
          "restrictedToMinimumLevel": "Warning",
          "fromEmail": "email@email.co,",
          "toEmail": "email@email.com",
          "mailServer": "smtp.email.com",
          "port": 587,
          "networkCredentials": {
            "UserName": "email@email.com",
            "Password": "password"
          }
        }
      }
    ]
  }

Any insight of how to configure this settings?

Thanks,

EduVZ commented 6 years ago

adding quotes around the port no did the trick for me: "port": "587"

Regards Edu,

qwertyuiope commented 6 years ago

I just tried adding quotes and didnt work... it keeps showing me port 25 instead of 587

andriysavin commented 6 years ago

I doubt you can configure it from a file or other source at least because networkCredentials is an interface. How would configuration code know which implementation to use?

ArnaudB88 commented 5 years ago

Is it possible to provide such functionality?

andriysavin commented 5 years ago

@ArnaudB88 you can work around this problem by defining your own extension method with your own settings type which doesn't have interfaces, like this:

public static LoggerConfiguration Email(
            this LoggerSinkConfiguration loggerConfiguration,
            MyEmailConnectionInfo emailConnectionInfo,
            string outputTemplate = DefaultOutputTemplate,
            LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose,
            int batchPostingLimit = 100,
            TimeSpan? period = default,
            IFormatProvider formatProvider = null,
            string mailSubject = "Log Email")
        {
             var emailConnectionInfo =
                myEmailConnectionInfo.ToSerilogEmailConnectionInfo();

            return loggerConfiguration.Email(
                emailConnectionInfo,
                outputTemplate,
                restrictedToMinimumLevel,
                batchPostingLimit,
                period,
                formatProvider,
                mailSubject);
        }
ArnaudB88 commented 5 years ago

@andriysavin My workaround for now is manually creating a config section for email logging (with credentials) and manually configuring the logging email sink with data from the custom config section. But it would be much more convenient to use the existing way of configuring an email sink, with the added properties 'username' and 'password'.

andriysavin commented 5 years ago

I meant that extension method would be called automatically so you don't have to write that manual code. Just as you would expect from out-of-the-box functionality. The only requirement is that your assembly contains Serilog in it's name or it's referenced explicitly in the config via Using.

aatmmr commented 5 years ago

I can tell that "port": 587 works for my project (netcoreapp2.1) using

JDeman commented 3 years ago

For me, the "Port" and "EnableSsl" options are always set to 25 & false ... Any updates on this? "Serilog.AspNetCore" Version="3.4.0" "Serilog.Sinks.Email" Version="2.4.0"

shagaroo commented 2 years ago

For me, adding connectionInfo after args worked to configure port. But I can't seem to configure the network credentials:

"WriteTo": [
  {
    "Name": "Email",
    "Args": {
      "connectionInfo": {
        "restrictedToMinimumLevel": "Warning",
        "fromEmail": "email@email.co,",
        "toEmail": "email@email.com",
        "mailServer": "smtp.email.com",
        "port": 587,
        "networkCredentials": {
          "UserName": "email@email.com",
          "Password": "password"
        }
      }
    }
  }
]

}

shagaroo commented 2 years ago

See this regarding getting credentials from JSON config file.

nblumhardt commented 9 months ago

3.0.0-dev-* now uses MailKit on all TFMs and will accept these through the regular configuration system. HTH! Closing this one as stale.