Added additional logging

This commit is contained in:
Alex Hyett 2018-12-12 13:35:55 +00:00
parent 24b93875fd
commit 779bad518b
4 changed files with 39 additions and 7 deletions

View file

@ -10,6 +10,9 @@ using MimeKit;
using MimeKit.Text;
using Serilog;
using Serilog.Context;
using SerilogTimings.Extensions;
namespace ContactForm
{
@ -31,6 +34,14 @@ namespace ContactForm
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();
emailBody.AppendLine($"Name: {input.Name}");
emailBody.AppendLine($"Email: {input.Email}");
@ -38,7 +49,10 @@ namespace ContactForm
emailBody.AppendLine($"Website: {input.Website}");
emailBody.AppendLine($"Message: {input.Body}");
await SendEmail(emailBody.ToString());
using(_logger.TimeOperation("Sending Email"))
{
await SendEmail(emailBody.ToString());
}
}
public async Task SendEmail(string body)

View file

@ -20,6 +20,7 @@
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0"/>
<PackageReference Include="MimeKit" Version="2.1.0"/>
<PackageReference Include="MailKit" Version="2.1.0.3"/>
<PackageReference Include="SerilogTimings" Version="2.2.0"/>
</ItemGroup>
<ItemGroup>
<None Include="appsettings.json" CopyToPublishDirectory="Always" CopyToOutputDirectory="PreserveNewest"/>

View file

@ -15,6 +15,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog;
using Serilog.Context;
// 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))]
@ -51,13 +52,29 @@ namespace ContactForm
.Destructure.AsScalar<JArray>()
.CreateLogger();
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
try
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();
var appService = serviceProvider.GetService<App>();
var serviceProvider = serviceCollection.BuildServiceProvider();
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();
return new { location = "https://www.alexhyett.com" };

View file

@ -27,7 +27,7 @@
],
"Enrich": ["FromLogContext"],
"Properties": {
"ApplicationName": "Lambda.Example"
"ApplicationName": "Lambda.Contact"
}
}
}