serilog / serilog-sinks-email

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

Emails not sending and code not hitting break point #120

Closed danammeansbear closed 11 months ago

danammeansbear commented 1 year ago

I am trying to send the logs that are being logged in my log file over to my email address.

The logging currently send to a file but now I need it to send to my email and I am not sure what I am doing wrong? I have pasted the code below

` public static class SerilogCustomEmailExtension { const string DefaultOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}";

    public static LoggerConfiguration CustomEmail(
        this LoggerSinkConfiguration loggerConfiguration,
        CustomEmailConnectionInfo connectionInfo,
        string outputTemplate = DefaultOutputTemplate,
        LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
    )
    {
        return loggerConfiguration.Email(
            connectionInfo,
            outputTemplate,
            restrictedToMinimumLevel
        );
    }

    public class CustomEmailConnectionInfo : EmailConnectionInfo
    {
        public CustomEmailConnectionInfo()
        {
            NetworkCredentials = new NetworkCredential();
        }
    }
}`

I have that code in my MVC Controller

`// "path": "C:/somedirectory/logs/website.log", "Serilog": { "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Email", "SomeFileName" ], "MinimumLevel": { "Default": "Debug", "Override": { "Microsoft": "Warning", "System": "Warning" } },

"WriteTo": [
  {
    "Using": [ "Serilog.Sinks.Email", "SomeFileName" ],
    "Name": "EmailCustom",
    "Args": {
      "fromEmail": "some@email.com",
      "toEmail": "some@email.com",
      "enableSsl": true,
      "mailSubject": "Requests Error",
      "isBodyHtml": false,
      "MailServer": "someServer@email.com",
      "Port": 25,
      "outputTemplate": "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}",
      "batchPostingLimit": 1,
      "restrictedToMinimumLevel": "Error"
    }
  },
  { "Name": "Console" },
  {
    "Name": "File",
    "Args": {
      "path": "C:/somedirectory/logs/website.log",
      "rollingInterval": "Day"
    }
  }
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]

}` I have the above in my appsettings.json

`public class Program { public static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Verbose() .Enrich.With(new ThreadIdEnricher()) .WriteTo.File("C:/someDirectory/website.log", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") .WriteTo.Console( outputTemplate: "{Timestamp:HH:mm} [{Level}] ({ThreadId}) {Message}{NewLine}{Exception}") .WriteTo.Email(new EmailConnectionInfo { FromEmail = "noreply@email.com", ToEmail = "some@email.com", MailServer = "someEmailServer.com", //NetworkCredentials = new NetworkCredential //{ // UserName = appConfigs.Logger.EmailSettings.Username, // Password = appConfigs.Logger.EmailSettings.Password //}, EnableSsl = true, Port = 25, EmailSubject = "Requests Error" }, outputTemplate: "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}", batchPostingLimit: 10 , restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Verbose ) .CreateLogger();

        //var logger = new LoggerConfiguration().ReadFrom.AppSettings("appsettings.json").CreateLogger();
        try 
        {
           // Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
            CreateHostBuilder(args).Build().Run();
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Application terminated unexpectedly");

        }
        finally
        {
            //Log.Information("Testing...serilog...");
            //Log.CloseAndFlush();
        }

    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
       .UseSerilog((hostBuilderContext, loggerConfiguration) =>
        loggerConfiguration.ReadFrom.Configuration(hostBuilderContext.Configuration));

}`
nblumhardt commented 11 months ago

Hi! I can't spot anything obvious in your example above. Checking SelfLog would be the way to go, if you're still digging into this. Closing as stale, but if you still need help, tagging with serilog on Stack Overflow will get the right eyes on it 👍