Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 11 logging implementation #31

Merged
merged 3 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,4 @@ ASALocalRun/
/build/content-repo
/WebApi/Eisk.WebApi/Properties/ServiceDependencies/eisk-webapi-1 - Web Deploy/profile.arm.json
/build/dnn-remote-template-render
*.txt
2 changes: 2 additions & 0 deletions Core/Eisk.Core/Eisk.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" />
<PackageReference Include="Serilog.Builder" Version="1.4.1-preview.9" />
<PackageReference Include="Serilog.Sinks.Http" Version="8.0.0" />
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions WebApi/Eisk.WebApi/Configuration/Serilog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Serilog;

namespace Eisk.WebApi.Configuration
{
public static class Serilog
{
private static readonly Func<IConfigurationRoot, ConfigurationManager, LoggerConfiguration> _serilogConfig = (config, env)
=> new LoggerConfiguration()
.WriteTo.Seq(string.IsNullOrWhiteSpace(env["sqlServerUrl"]) ? "http://seq" : env["sqlServerUrl"])
.WriteTo.Http(requestUri: string.IsNullOrWhiteSpace(env["requestUri"]) ? "http://logstash:8080" : env["requestUri"], queueLimitBytes: null)
.ReadFrom.Configuration(config);

private static readonly IConfigurationRoot _configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
.Build();

public static void AddSerilogConfig(this WebApplicationBuilder builder, ConfigurationManager env)
=> builder
.Logging
.AddSerilog(_serilogConfig(_configurationBuilder, env).CreateLogger());
}
}
4 changes: 4 additions & 0 deletions WebApi/Eisk.WebApi/Eisk.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Serilog.Sinks.Http" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

Expand Down
6 changes: 6 additions & 0 deletions WebApi/Eisk.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
using Eisk.DataServices.Interfaces;
using Eisk.DomainServices;
using Eisk.EFCore.Setup;
using Eisk.WebApi.Configuration;
using Eisk.WebApi.Middlewares;
using Microsoft.OpenApi.Models;
using Serilog;

var builder = WebApplication.CreateBuilder(args);


builder.AddSerilogConfig(builder.Configuration);

Log.Information("Configuring web host...");
// Add services to the container.

builder.Services.AddControllers();
Expand Down
48 changes: 44 additions & 4 deletions WebApi/Eisk.WebApi/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,48 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console",
"DiagnosticTraceSink": { "Name": "DiagnosticTrace" }
},
{
"Name": "File",
"Args": { "path": "Logs/log.txt" }
}
],
"WriteTo:Async": {
"Name": "Async",
"Args": {
"configure": [
{
"Name": "File",
"Args": {
"path": "%TEMP%/Logs/serilog-configuration-sample.txt",
"outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}"
}
}
]
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Destructure": [
{
"Name": "ToMaximumDepth",
"Args": { "maximumDestructuringDepth": 4 }
},
{
"Name": "ToMaximumStringLength",
"Args": { "maximumStringLength": 100 }
},
{
"Name": "ToMaximumCollectionCount",
"Args": { "maximumCollectionCount": 10 }
}
],
"Properties": {
"Application": "Eisk.WebApi"
}
}
}
2 changes: 1 addition & 1 deletion WebApi/Eisk.WebApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
},
Expand Down