serilog / serilog-sinks-email

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

Not able to send emails without "using" formula #75

Closed JethroCZ closed 11 months ago

JethroCZ commented 4 years ago

Hello, I have a question, how to configure Email sink to make it sure it sends all emails before app is closed, using WindsorContainer, LoggingFacilities, Serilog and Serilog.Sinks.Email? The problem I am experiencing is as this: I have created test project where I am using WindsorContainer with Logging facility:

container.AddFacility<LoggingFacility>(f => 
                f.LogUsing(
                    new SerilogFactory(
                        new LoggerConfiguration().ReadFrom.Configuration(
                            container.Resolve<SerilogConfig>().GetConfiguration()).CreateLogger())));
// SerilogConfig just creates IConfiguration from given JSON file
// Here is the JSON file
{
  "Serilog": {
    "Using": [ "Shared", "Serilog.Enrichers.AssemblyName", "Serilog.Enrichers.Environment", "Serilog.Enrichers.Thread" ],
    "MinimumLevel": {
      "Default": "Verbose"
    },
    "Enrich": [ "WithLoggedUser", "WithServerStart", "WithOperationSystem", "WithAssemblyVersion", "WithMachineName", "WithThreadId", "FromLogContext" ],
    "SelfLog": "Enable",
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss,fff} [{ThreadId:d2}] {Level:u5} [{ndc:u8}] usr:{LoggedUser:u10} {Message:lj}{NewLine} Properties:{NewLine} {Properties}{NewLine} Exception:{NewLine} {Exception}{NewLine}"
        }
      },
      {
        "Name": "File",
        "Args": {
          "Path": "%programdata%\\SerilogTest\\WebAPI-log.txt",
          "fileSizeLimitBytes": "20971520",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss,fff} [{ThreadId:d2}] {Level:u5} [{ndc:u8}] usr:{LoggedUser:u10} {Message:lj}{NewLine} Properties:{NewLine} {Properties}{NewLine} Exception:{NewLine} {Exception}{NewLine}"
        }
      },
      {
        "Name": "Email",
        "Args": {
          "connectionInfo": {
            "FromEmail": "Sender",
            "ToEmail": "Reciver",
            "MailServer": "MailServer",
            "EmailSubject": "Fatal or error",
            "Port": 25
          },
          "restrictedToMinimumLevel": "Error",
          "outputTemplate": "[{Timestamp:HH:mm:ss.fff}]{NewLine} [Level:{Level:u3}]{NewLine}[Thread:{ThreadId:d2}]{NewLine} Message:{NewLine} {Message:lj}{NewLine} Exception:{NewLine} {Exception}"
        }
      }
    ]
  }
}

The problem is, when I run it in ordinary console app, my logs are logged into console and file sink, but not into Email sink. I was not able to find any solution to this problem other than create logger with "using" formula:

using (var log = new LoggerConfiguration().WriteTo
                .Email("Sender Email", 
                          "Reciever email",
                          mailServer: "My mail server",
                          batchPostingLimit: 1)              // This seems to not work without "using" as well 
                .CreateLogger())
            {
                log.Error("First error");
                log.Error("Second error");
            }

Is there something obvious I dont know, it might be WindsorContainer configuration as well. (I am not much experienced with those technologies) The project is on my GitHub here.

weirdyang commented 2 years ago

Could it be the misspelled Receiver in the email section of the json?

JethroCZ commented 2 years ago

Well it's more than 2 years :) "Sender Email" and "Receiver Email" is just text which I used to hide real addresses for this issue.

nblumhardt commented 11 months ago

Closing this one as stale, hope you were able to find a resolution 👍