BackgroundService Serilog how to email?

Cenk 956 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@test.com",
          "toEmail": "test1@gmail.com",
          "enableSsl": false,
          "mailSubject": "Requests Error",
          "isBodyHtml": false,
          "mailServer": "smtp.yandex.com.tr",
          "networkCredentialuserName": "test@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"
    
  }
}


ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,187 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,275 questions
{count} votes