Added additional logging
This commit is contained in:
parent
24b93875fd
commit
779bad518b
4 changed files with 39 additions and 7 deletions
|
@ -10,6 +10,9 @@ using MimeKit;
|
||||||
using MimeKit.Text;
|
using MimeKit.Text;
|
||||||
|
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
using Serilog.Context;
|
||||||
|
|
||||||
|
using SerilogTimings.Extensions;
|
||||||
|
|
||||||
namespace ContactForm
|
namespace ContactForm
|
||||||
{
|
{
|
||||||
|
@ -31,6 +34,14 @@ namespace ContactForm
|
||||||
|
|
||||||
public async Task Run(ContactRequest input)
|
public async Task Run(ContactRequest input)
|
||||||
{
|
{
|
||||||
|
using(LogContext.PushProperty("Email", input.Email))
|
||||||
|
using(LogContext.PushProperty("Phone", input.Phone))
|
||||||
|
using(LogContext.PushProperty("Website", input.Website))
|
||||||
|
using(LogContext.PushProperty("Body", input.Body))
|
||||||
|
{
|
||||||
|
_logger.Information("Contact Message Received from {Name}", input.Name);
|
||||||
|
}
|
||||||
|
|
||||||
var emailBody = new StringBuilder();
|
var emailBody = new StringBuilder();
|
||||||
emailBody.AppendLine($"Name: {input.Name}");
|
emailBody.AppendLine($"Name: {input.Name}");
|
||||||
emailBody.AppendLine($"Email: {input.Email}");
|
emailBody.AppendLine($"Email: {input.Email}");
|
||||||
|
@ -38,7 +49,10 @@ namespace ContactForm
|
||||||
emailBody.AppendLine($"Website: {input.Website}");
|
emailBody.AppendLine($"Website: {input.Website}");
|
||||||
emailBody.AppendLine($"Message: {input.Body}");
|
emailBody.AppendLine($"Message: {input.Body}");
|
||||||
|
|
||||||
await SendEmail(emailBody.ToString());
|
using(_logger.TimeOperation("Sending Email"))
|
||||||
|
{
|
||||||
|
await SendEmail(emailBody.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendEmail(string body)
|
public async Task SendEmail(string body)
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0"/>
|
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0"/>
|
||||||
<PackageReference Include="MimeKit" Version="2.1.0"/>
|
<PackageReference Include="MimeKit" Version="2.1.0"/>
|
||||||
<PackageReference Include="MailKit" Version="2.1.0.3"/>
|
<PackageReference Include="MailKit" Version="2.1.0.3"/>
|
||||||
|
<PackageReference Include="SerilogTimings" Version="2.2.0"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="appsettings.json" CopyToPublishDirectory="Always" CopyToOutputDirectory="PreserveNewest"/>
|
<None Include="appsettings.json" CopyToPublishDirectory="Always" CopyToOutputDirectory="PreserveNewest"/>
|
||||||
|
|
|
@ -15,6 +15,7 @@ using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
using Serilog.Context;
|
||||||
|
|
||||||
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
|
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
|
||||||
[assembly : LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
|
[assembly : LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
|
||||||
|
@ -51,13 +52,29 @@ namespace ContactForm
|
||||||
.Destructure.AsScalar<JArray>()
|
.Destructure.AsScalar<JArray>()
|
||||||
.CreateLogger();
|
.CreateLogger();
|
||||||
|
|
||||||
var serviceCollection = new ServiceCollection();
|
try
|
||||||
ConfigureServices(serviceCollection);
|
{
|
||||||
|
var serviceCollection = new ServiceCollection();
|
||||||
|
ConfigureServices(serviceCollection);
|
||||||
|
|
||||||
var serviceProvider = serviceCollection.BuildServiceProvider();
|
var serviceProvider = serviceCollection.BuildServiceProvider();
|
||||||
var appService = serviceProvider.GetService<App>();
|
var appService = serviceProvider.GetService<App>();
|
||||||
|
|
||||||
appService.Run(input).GetAwaiter().GetResult();
|
appService.Run(input).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
using(LogContext.PushProperty("Name", input.Email))
|
||||||
|
using(LogContext.PushProperty("Email", input.Email))
|
||||||
|
using(LogContext.PushProperty("Phone", input.Phone))
|
||||||
|
using(LogContext.PushProperty("Website", input.Website))
|
||||||
|
using(LogContext.PushProperty("Body", input.Body))
|
||||||
|
{
|
||||||
|
_logger.Error(ex, "Error sending email from contact form");
|
||||||
|
}
|
||||||
|
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
Log.CloseAndFlush();
|
Log.CloseAndFlush();
|
||||||
return new { location = "https://www.alexhyett.com" };
|
return new { location = "https://www.alexhyett.com" };
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
],
|
],
|
||||||
"Enrich": ["FromLogContext"],
|
"Enrich": ["FromLogContext"],
|
||||||
"Properties": {
|
"Properties": {
|
||||||
"ApplicationName": "Lambda.Example"
|
"ApplicationName": "Lambda.Contact"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue