public static LoggerConfiguration EmailCustom(
     this LoggerSinkConfiguration sinkConfiguration,
     string fromEmail,
     string toEmail,
     string enableSsl,
     string mailSubject,
     string isBodyHtml,
     string mailServer,
     string networkCredentialuserName,
     string networkCredentialpassword,
     string smtpPort,
     string outputTemplate,
     string batchPostingLimit,
     string periodMinutes,
     string restrictedToMinimumLevel)
 {
     return(sinkConfiguration.Email(
                new EmailConnectionInfo
     {
         FromEmail = fromEmail,
         ToEmail = toEmail,
         EnableSsl = GetBoolean(enableSsl),
         EmailSubject = mailSubject,
         IsBodyHtml = GetBoolean(isBodyHtml),
         MailServer = mailServer,
         NetworkCredentials = new NetworkCredential(networkCredentialuserName, networkCredentialpassword),
         Port = GetInt(smtpPort),
     },
                outputTemplate,
                GetLevel(restrictedToMinimumLevel),
                GetInt(batchPostingLimit),
                TimeSpan.FromMinutes(GetInt(periodMinutes))));
 }
        internal static LoggerConfiguration Email(this LoggerSinkConfiguration loggerSinkConfiguration, MailConfig mailConfig)
        {
            port               = mailConfig.Port;
            enableSsl          = mailConfig.EnableSsl;
            toEmails           = mailConfig.ToEmails;
            server             = mailConfig.Server;
            networkCredentials = mailConfig.NetworkCredentials;


            var emailInfo = new EmailConnectionInfo
            {
                FromEmail    = networkCredentials.UserName,
                ToEmail      = string.Join(';', toEmails),
                MailServer   = server,
                EmailSubject = subject,
                EnableSsl    = enableSsl,
                Port         = port
            };

            if (!string.IsNullOrEmpty(networkCredentials.Password))
            {
                emailInfo.NetworkCredentials = networkCredentials;
            }
            AddSerilogEmailFailsafe();

            return(loggerSinkConfiguration.Email(emailInfo, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error));
        }
Beispiel #3
0
        public static LoggerConfiguration Email(
            this LoggerSinkConfiguration loggerConfiguration,
            string fromEmail,
            string toEmail,
            string mailServer,
            string mailUserName,
            string mailPassword,
            bool enableSsl     = false,
            int port           = 25,
            string mailSubject = EmailConnectionInfo.DefaultSubject,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (fromEmail == null)
            {
                throw new ArgumentNullException(nameof(fromEmail));
            }
            if (toEmail == null)
            {
                throw new ArgumentNullException(nameof(toEmail));
            }

            var connectionInfo = new EmailConnectionInfo
            {
                FromEmail          = fromEmail,
                ToEmail            = toEmail,
                MailServer         = mailServer,
                Port               = port,
                NetworkCredentials = new NetworkCredential(mailUserName, mailPassword),
                EmailSubject       = mailSubject,
                EnableSsl          = enableSsl,
                ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) =>
                {
                    return(true);
                }
            };

            return(loggerConfiguration.Email(connectionInfo, restrictedToMinimumLevel: restrictedToMinimumLevel));
        }
Beispiel #4
0
        private static void Configue(LoggerSinkConfiguration configuration, SerilogOptions serilogOptions, Action <LoggerSinkConfiguration> configure = null)
        {
            var outputTemplate = serilogOptions.OutputTemplate ?? SerilogExtensions.DefaultOutputTemplate;

            JsonFormatter jsonFormatter = null;

            if (serilogOptions.FormatJson)
            {
                jsonFormatter = new JsonFormatter();
            }

            var obsoletePathFormat = serilogOptions.PathFormat;
            var path = serilogOptions.Path;

            //compatibility
            if (!string.IsNullOrWhiteSpace(obsoletePathFormat) && string.IsNullOrWhiteSpace(path))
            {
                path = obsoletePathFormat;
            }

            if (!string.IsNullOrWhiteSpace(path))
            {
                // whether relative path is used, the root directory is applied by default.
                if (!Path.IsPathRooted(path))
                {
#if NETFULL
                    path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
#else
                    path = Path.Combine(AppContext.BaseDirectory, path);
#endif
                }
            }
            else
            {
                //By default, the 'logs' directory of the root directory.
#if NETFULL
                path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", "log.log");
#else
                path = Path.Combine(AppContext.BaseDirectory, "logs", "log.log");
#endif
            }

            //stdout
            if (serilogOptions.StdOut)
            {
                if (serilogOptions.FormatJson)
                {
                    configuration.ConfigueStd(jsonFormatter);
                }
                else
                {
                    configuration.ConfigueStd(outputTemplate: outputTemplate);
                }
            }

            //file Record
            if (serilogOptions.RollingFile)
            {
                if (serilogOptions.FormatJson)
                {
                    configuration.ConfigueFile(jsonFormatter, path: path);
                }
                else
                {
                    configuration.ConfigueFile(path: path, outputTemplate: outputTemplate);
                }
            }

            //email
            if (serilogOptions.Email != null)
            {
                if (!string.IsNullOrWhiteSpace(serilogOptions.Email.Account) && !string.IsNullOrWhiteSpace(serilogOptions.Email.Password))
                {
                    serilogOptions.Email.NetworkCredentials = new NetworkCredential(serilogOptions.Email.Account, serilogOptions.Email.Password);
                }

                configuration.Email(serilogOptions.Email, outputTemplate: outputTemplate, restrictedToMinimumLevel: LogEventLevel.Warning);
            }

            configure?.Invoke(configuration);
        }