Skip to content
This repository has been archived by the owner on Apr 8, 2023. It is now read-only.

Commit

Permalink
Clean up serilog and sink to email. (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminsampica authored Jun 28, 2022
1 parent dcdb418 commit aa130a8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
path: '**/coverage/'
- name: Publish
if: startsWith(github.ref, 'refs/tags/v')
run: dotnet publish -c Release -o 'drop'
run: dotnet publish -c Release -o 'drop' s
- name: Variable Substitution
if: startsWith(github.ref, 'refs/tags/v')
uses: microsoft/variable-substitution@v1
Expand All @@ -47,6 +47,7 @@ jobs:
env:
ConnectionStrings.DefaultConnection: ${{ secrets.connectionstring }}
Email.Password: ${{ secrets.email_password }}
Email.AdminEmail: ${{ secrets.admin_email }}
- name: Create app_offline file
if: startsWith(github.ref, 'refs/tags/v')
shell: pwsh
Expand Down
3 changes: 2 additions & 1 deletion src/Server/DynamoLeagueBlazor.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.5" />
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
<PackageReference Include="Serilog.AspNetCore.Ingestion" Version="1.0.0-dev-00021" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Email" Version="2.4.0" />
<PackageReference Include="Serilog.Sinks.Trace" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 5 additions & 6 deletions src/Server/Infrastructure/EmailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ public class EmailSettings
{
public const string Email = "Email";

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public string MailServer { get; set; }
public string MailServer { get; set; } = null!;
public int MailPort { get; set; }
public string SenderName { get; set; }
public string Sender { get; set; }
public string Password { get; set; }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public string SenderName { get; set; } = null!;
public string Sender { get; set; } = null!;
public string Password { get; set; } = null!;
public string AdminEmail { get; set; }
}
42 changes: 26 additions & 16 deletions src/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,39 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Serilog;
using Serilog.Events;
using System.IdentityModel.Tokens.Jwt;
using System.Reflection;

try
{
var builder = WebApplication.CreateBuilder(args);

Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.Enrich.WithProperty("Environment", builder.Environment.EnvironmentName)
.CreateLogger();
var loggerConfiguration = new LoggerConfiguration()
.MinimumLevel.Is(LogEventLevel.Information)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Duende", LogEventLevel.Error)
.Enrich.FromLogContext()
.Enrich.WithProperty("Environment", builder.Environment.EnvironmentName);

builder.Services.Configure<EmailSettings>(builder.Configuration.GetSection(EmailSettings.Email))
.AddSingleton(s => s.GetRequiredService<IOptions<EmailSettings>>().Value);

if (builder.Environment.IsProduction())
{
builder.Services.AddSingleton<IEmailSender, EmailSender>();

var emailSettings = builder.Services.BuildServiceProvider().GetRequiredService<EmailSettings>();
loggerConfiguration.WriteTo.Email(emailSettings.Sender, emailSettings.AdminEmail, mailServer: emailSettings.MailServer, restrictedToMinimumLevel: LogEventLevel.Error, mailSubject: "An error occured on Dynamo League.");
}
else
{
builder.Services.AddSingleton<IEmailSender, DevelopmentEmailSender>();
loggerConfiguration.WriteTo.Trace();
}

Log.Logger = loggerConfiguration.CreateLogger();

builder.Host.UseSerilog();

Expand Down Expand Up @@ -77,18 +99,6 @@
builder.Services.AddTransient<IBidAmountValidator, BidAmountValidator>();
builder.Services.AddTransient<IPlayerHeadshotService, PlayerHeadshotService>();

builder.Services.Configure<EmailSettings>(builder.Configuration.GetSection(EmailSettings.Email))
.AddSingleton(s => s.GetRequiredService<IOptions<EmailSettings>>().Value);

if (builder.Environment.IsProduction())
{
builder.Services.AddSingleton<IEmailSender, EmailSender>();
}
else
{
builder.Services.AddSingleton<IEmailSender, DevelopmentEmailSender>();
}

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
29 changes: 2 additions & 27 deletions src/Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"MailPort": 25,
"SenderName": "Dynamo League - Fantasy Football",
"Sender": "[email protected]",
"Password": ""
"Password": "",
"AdminEmail": ""
},
"IdentityServer": {
"Clients": {
Expand All @@ -19,31 +20,5 @@
"Key": {
"Type": "Development"
}
},
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"System": "Warning",
"Microsoft": "Warning",
"Duende": "Error"
}
},
"WriteTo": [
{
"Name": "Trace"
},
{
"Name": "File",
"Args": {
"path": "./logs/log.log",
"outputTemplate": "[{Timestamp}] [{Level:u3}] [{Environment}] {Message}{NewLine}{Exception}",
"rollingInterval": "Day"
}
}
],
"Enrich": [
"FromLogContext"
]
}
}

0 comments on commit aa130a8

Please sign in to comment.