diff --git a/src/ContactForm/App.cs b/src/ContactForm/App.cs index 01f7757..763d6ea 100644 --- a/src/ContactForm/App.cs +++ b/src/ContactForm/App.cs @@ -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) diff --git a/src/ContactForm/ContactForm.csproj b/src/ContactForm/ContactForm.csproj index e21403e..f1b73d6 100644 --- a/src/ContactForm/ContactForm.csproj +++ b/src/ContactForm/ContactForm.csproj @@ -20,6 +20,7 @@ + diff --git a/src/ContactForm/Function.cs b/src/ContactForm/Function.cs index cc67dff..9973f18 100644 --- a/src/ContactForm/Function.cs +++ b/src/ContactForm/Function.cs @@ -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() .CreateLogger(); - var serviceCollection = new ServiceCollection(); - ConfigureServices(serviceCollection); + try + { + var serviceCollection = new ServiceCollection(); + ConfigureServices(serviceCollection); - var serviceProvider = serviceCollection.BuildServiceProvider(); - var appService = serviceProvider.GetService(); + var serviceProvider = serviceCollection.BuildServiceProvider(); + var appService = serviceProvider.GetService(); - 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" }; diff --git a/src/ContactForm/appsettings.json b/src/ContactForm/appsettings.json index 94900fa..74683b1 100644 --- a/src/ContactForm/appsettings.json +++ b/src/ContactForm/appsettings.json @@ -27,7 +27,7 @@ ], "Enrich": ["FromLogContext"], "Properties": { - "ApplicationName": "Lambda.Example" + "ApplicationName": "Lambda.Contact" } } }