BackgroundService Serilog how to email?

Cenk 1,036 Reputation points
2023-01-31T14:40:53.22+00:00

Hi,

In my background service, I would like to send error logs. Although there is an error, the email sink does not work. The error is being written to the file and mssqlserver successfully. Can you please share your experiences?

Here is the Program.cs

public class Program
    {
        public static void Main(string[] args)
        {
            IHost host = Host.CreateDefaultBuilder(args)
                .UseSerilog()
                .ConfigureServices(services =>
                {
                    services.AddHttpClient();
                    services.AddHostedService<Worker>();
                })
                .Build();
            Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(host.Services.GetRequiredService<IConfiguration>()).CreateLogger();

            host.Run();
        }
    }
{
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "Default": "Server =.\\SQLEXPRESS; Database=TestAPI;Integrated Security=True;  MultipleActiveResultSets=True; Trusted_Connection=yes; Encrypt=False"
  },
  "Razer": {
    "Production": "http://localhost:5236/api/requests"
  },
  "Serilog": {
    "MinimumLevel": "Information",
    "Override": {
      "Microsoft.AspNetCore": "Warning"
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "{Timestamp:HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "Serilogs\\AppLogs.log",
          "restrictedToMinimumLevel": "Error",
          "buffered": false,
          "rollingInterval": "Day",
          "retainedFileCountLimit": 3
        }
      },
      {
        "Name": "EmailCustom",
        "Args": {
          "fromEmail": "******@test.com",
          "toEmail": "******@gmail.com",
          "enableSsl": false,
          "mailSubject": "Requests Error",
          "isBodyHtml": false,
          "mailServer": "smtp.yandex.com.tr",
          "networkCredentialuserName": "******@test.com",
          "networkCredentialpassword": "123456",
          "smtpPort": 587,
          "outputTemplate": "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}",
          "batchPostingLimit": 1,
          "restrictedToMinimumLevel": "Error"
        }
      },
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "Server =.\\SQLEXPRESS; Database=TestAPI;Integrated Security=True;  MultipleActiveResultSets=True; Trusted_Connection=yes; Encrypt=False",
          "tableName": "RequestsExceptionLog",
          "autoCreateSqlTable": true,
          "restrictedToMinimumLevel": "Error",
          "batchPostingLimit": 50,
          "period": "0.00:00:20"

        }
      }
    ],
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithProcessId",
      "WithThreadId"
    ]
  },
  "Token": {
    "Production": "http://localhost:5236/api/token"
    
  }
}


Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.