From 7179551d1380beac89558a915e082ff9719a7c19 Mon Sep 17 00:00:00 2001 From: Alex Hyett Date: Mon, 10 Dec 2018 14:41:08 +0000 Subject: [PATCH] Added Serilog and Seq --- docker-compose.yml | 9 ++++++ src/lambda-dotnet-console/App.cs | 15 ++++++++-- src/lambda-dotnet-console/Function.cs | 28 +++++++++++++++---- src/lambda-dotnet-console/appsettings.json | 23 +++++++++++++++ .../lambda-dotnet-console.csproj | 4 +++ .../appsettings.json | 23 +++++++++++++++ 6 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..214df50 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3' + +services: + seq: + image: datalust/seq:latest + ports: + - 5341:80 + environment: + ACCEPT_EULA: Y diff --git a/src/lambda-dotnet-console/App.cs b/src/lambda-dotnet-console/App.cs index f956630..d811d3e 100644 --- a/src/lambda-dotnet-console/App.cs +++ b/src/lambda-dotnet-console/App.cs @@ -1,22 +1,31 @@ using System; +using Serilog; + namespace lambda_dotnet_console { public class App { - private AppSettings _settings; + private readonly AppSettings _settings; + private readonly ILogger _logger; public App( - AppSettings settings + AppSettings settings, + ILogger logger ) { _settings = settings ?? throw new ArgumentNullException(nameof(settings)); + _logger = logger ?? + throw new ArgumentNullException(nameof(settings)); } public string Run(string input) { - return $"{_settings.Prefix}{input?.ToUpper()}"; + _logger.Information("Lambda Function Starting"); + var result = $"{_settings.Prefix}{input?.ToUpper()}"; + _logger.Information("Lambda Function Finished"); + return result; } } } \ No newline at end of file diff --git a/src/lambda-dotnet-console/Function.cs b/src/lambda-dotnet-console/Function.cs index e6a60ac..435e36c 100644 --- a/src/lambda-dotnet-console/Function.cs +++ b/src/lambda-dotnet-console/Function.cs @@ -9,6 +9,10 @@ using Amazon.Lambda.Core; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json.Linq; + +using Serilog; + // 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))] @@ -16,6 +20,8 @@ namespace lambda_dotnet_console { public class Function { + private AppSettings _appSettings; + private ILogger _logger; /// /// Lamda Function @@ -31,20 +37,30 @@ namespace lambda_dotnet_console .AddEnvironmentVariables(prefix: "LAMBDA_") .Build(); + _appSettings = new AppSettings(); + configuration.GetSection("App").Bind(_appSettings); + + _logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .Destructure.AsScalar() + .Destructure.AsScalar() + .CreateLogger(); + var serviceCollection = new ServiceCollection(); ConfigureServices(serviceCollection); - var appSettings = new AppSettings(); - configuration.GetSection("App").Bind(appSettings); - serviceCollection.AddSingleton(appSettings); - var serviceProvider = serviceCollection.BuildServiceProvider(); - return serviceProvider.GetService().Run(input); + var result = serviceProvider.GetService().Run(input); + Log.CloseAndFlush(); + return result; } - private static void ConfigureServices(IServiceCollection serviceCollection) + private void ConfigureServices(IServiceCollection serviceCollection) { serviceCollection.AddTransient(); + serviceCollection.AddSingleton(_appSettings); + serviceCollection.AddSingleton(_logger); + serviceCollection.AddLogging(logBuilder => logBuilder.AddSerilog(dispose: true)); } } diff --git a/src/lambda-dotnet-console/appsettings.json b/src/lambda-dotnet-console/appsettings.json index 591872f..7a7dc0c 100644 --- a/src/lambda-dotnet-console/appsettings.json +++ b/src/lambda-dotnet-console/appsettings.json @@ -1,5 +1,28 @@ { "App": { "Prefix": "Test" + }, + "Serilog": { + "Using": ["Serilog.Sinks.Seq"], + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Warning", + "System": "Warning" + } + }, + "WriteTo": [ + { + "Name": "Seq", + "Args": { + "serverUrl": "http://localhost:5341", + "restrictedToMinimumLevel": "Information" + } + } + ], + "Enrich": ["FromLogContext"], + "Properties": { + "ApplicationName": "Lambda.Example" + } } } diff --git a/src/lambda-dotnet-console/lambda-dotnet-console.csproj b/src/lambda-dotnet-console/lambda-dotnet-console.csproj index 5644179..b6d202d 100644 --- a/src/lambda-dotnet-console/lambda-dotnet-console.csproj +++ b/src/lambda-dotnet-console/lambda-dotnet-console.csproj @@ -14,6 +14,10 @@ + + + + diff --git a/test/lambda-dotnet-console.Tests/appsettings.json b/test/lambda-dotnet-console.Tests/appsettings.json index 591872f..7a7dc0c 100644 --- a/test/lambda-dotnet-console.Tests/appsettings.json +++ b/test/lambda-dotnet-console.Tests/appsettings.json @@ -1,5 +1,28 @@ { "App": { "Prefix": "Test" + }, + "Serilog": { + "Using": ["Serilog.Sinks.Seq"], + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Warning", + "System": "Warning" + } + }, + "WriteTo": [ + { + "Name": "Seq", + "Args": { + "serverUrl": "http://localhost:5341", + "restrictedToMinimumLevel": "Information" + } + } + ], + "Enrich": ["FromLogContext"], + "Properties": { + "ApplicationName": "Lambda.Example" + } } }