Added Serilog and Seq

This commit is contained in:
Alex Hyett 2018-12-10 14:41:08 +00:00
parent c75cb84abb
commit 7179551d13
6 changed files with 93 additions and 9 deletions

9
docker-compose.yml Normal file
View file

@ -0,0 +1,9 @@
version: '3'
services:
seq:
image: datalust/seq:latest
ports:
- 5341:80
environment:
ACCEPT_EULA: Y

View file

@ -1,22 +1,31 @@
using System; using System;
using Serilog;
namespace lambda_dotnet_console namespace lambda_dotnet_console
{ {
public class App public class App
{ {
private AppSettings _settings; private readonly AppSettings _settings;
private readonly ILogger _logger;
public App( public App(
AppSettings settings AppSettings settings,
ILogger logger
) )
{ {
_settings = settings ?? _settings = settings ??
throw new ArgumentNullException(nameof(settings)); throw new ArgumentNullException(nameof(settings));
_logger = logger ??
throw new ArgumentNullException(nameof(settings));
} }
public string Run(string input) 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;
} }
} }
} }

View file

@ -9,6 +9,10 @@ using Amazon.Lambda.Core;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; 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 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))]
@ -16,6 +20,8 @@ namespace lambda_dotnet_console
{ {
public class Function public class Function
{ {
private AppSettings _appSettings;
private ILogger _logger;
/// <summary> /// <summary>
/// Lamda Function /// Lamda Function
@ -31,20 +37,30 @@ namespace lambda_dotnet_console
.AddEnvironmentVariables(prefix: "LAMBDA_") .AddEnvironmentVariables(prefix: "LAMBDA_")
.Build(); .Build();
_appSettings = new AppSettings();
configuration.GetSection("App").Bind(_appSettings);
_logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Destructure.AsScalar<JObject>()
.Destructure.AsScalar<JArray>()
.CreateLogger();
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection); ConfigureServices(serviceCollection);
var appSettings = new AppSettings();
configuration.GetSection("App").Bind(appSettings);
serviceCollection.AddSingleton<AppSettings>(appSettings);
var serviceProvider = serviceCollection.BuildServiceProvider(); var serviceProvider = serviceCollection.BuildServiceProvider();
return serviceProvider.GetService<App>().Run(input); var result = serviceProvider.GetService<App>().Run(input);
Log.CloseAndFlush();
return result;
} }
private static void ConfigureServices(IServiceCollection serviceCollection) private void ConfigureServices(IServiceCollection serviceCollection)
{ {
serviceCollection.AddTransient<App>(); serviceCollection.AddTransient<App>();
serviceCollection.AddSingleton<AppSettings>(_appSettings);
serviceCollection.AddSingleton<ILogger>(_logger);
serviceCollection.AddLogging(logBuilder => logBuilder.AddSerilog(dispose: true));
} }
} }

View file

@ -1,5 +1,28 @@
{ {
"App": { "App": {
"Prefix": "Test" "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"
}
} }
} }

View file

@ -14,6 +14,10 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0"/> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0"/> <PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.2.0"/> <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.2.0"/>
<PackageReference Include="Serilog" Version="2.7.1"/>
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2"/>
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1"/>
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="appsettings.json" CopyToPublishDirectory="Always" CopyToOutputDirectory="PreserveNewest"/> <None Include="appsettings.json" CopyToPublishDirectory="Always" CopyToOutputDirectory="PreserveNewest"/>

View file

@ -1,5 +1,28 @@
{ {
"App": { "App": {
"Prefix": "Test" "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"
}
} }
} }