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 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,8 +49,11 @@ namespace ContactForm
emailBody.AppendLine($"Website: {input.Website}"); emailBody.AppendLine($"Website: {input.Website}");
emailBody.AppendLine($"Message: {input.Body}"); emailBody.AppendLine($"Message: {input.Body}");
using(_logger.TimeOperation("Sending Email"))
{
await SendEmail(emailBody.ToString()); await SendEmail(emailBody.ToString());
} }
}
public async Task SendEmail(string body) public async Task SendEmail(string body)
{ {

View file

@ -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"/>

View file

@ -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,6 +52,8 @@ namespace ContactForm
.Destructure.AsScalar<JArray>() .Destructure.AsScalar<JArray>()
.CreateLogger(); .CreateLogger();
try
{
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection); ConfigureServices(serviceCollection);
@ -58,6 +61,20 @@ namespace ContactForm
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" };

View file

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