Skip to content

Commit

Permalink
perf: move configs to appsettings.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Azaferany committed Jul 26, 2022
1 parent 081b5b3 commit c8d1c77
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 41 deletions.
19 changes: 3 additions & 16 deletions src/QuickstartTemplate.WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
using QuickstartTemplate.WebApi;
using Sentry;
using Serilog;
using Serilog.Events;

var builder = WebApplication.CreateBuilder(args);
// NOTE: Default application configuration sources can be found at
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0#default-application-configuration-sources

builder.WebHost.UseSentry((builderContext, sentryOptions) =>
{
sentryOptions.Dsn = builderContext.Configuration["SENTRY_DSN"];
sentryOptions.AddExceptionFilterForType<OperationCanceledException>();
});

builder.Host.UseSerilog((context, configuration) =>
{
if (!Enum.TryParse(context.Configuration["LogEventLevel"], out LogEventLevel logEventLevel))
logEventLevel = LogEventLevel.Information;

configuration.Filter.ByExcluding(logEvent =>
logEvent.Exception != null && logEvent.Exception.GetType() == typeof(OperationCanceledException));

configuration.MinimumLevel.Is(logEventLevel);

configuration.Enrich.FromLogContext();

configuration.WriteTo.Console();
configuration.WriteTo.Sentry();
});
builder.Host.UseSerilog((context, configuration) => configuration.ReadFrom.Configuration(context.Configuration));

// Manually create an instance of the Startup class
// https://andrewlock.net/exploring-dotnet-6-part-12-upgrading-a-dotnet-5-startup-based-app-to-dotnet-6/
Expand Down
19 changes: 5 additions & 14 deletions src/QuickstartTemplate.WebApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,12 @@ public void ConfigureServices(IServiceCollection services)
});

services.AddSwaggerGen();


//https://josef.codes/asp-net-core-6-http-logging-log-requests-responses/
services.AddHttpLogging(options =>
{
//dont log Response if grpc is added it will break; track bug in below issue
//https://github.com/dotnet/aspnetcore/issues/39317

HttpLoggingFields httpLoggingFields;
//https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.httplogging.httploggingfields?view=aspnetcore-6.0
if (!Enum.TryParse(_configuration["HttpLoggingFields"],
out httpLoggingFields))
httpLoggingFields = HttpLoggingFields.None;
options.LoggingFields = httpLoggingFields;
});
//for more information read
//https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-logging/?view=aspnetcore-6.0
//dont log Response if grpc is added it will break; track bug in below issue
//https://github.com/dotnet/aspnetcore/issues/39317
services.AddHttpLogging(options => _configuration.Bind("HttpLogging", options));
}

public void Configure(WebApplication app)
Expand Down
34 changes: 28 additions & 6 deletions src/QuickstartTemplate.WebApi/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
"Serilog": {
"MinimumLevel": {
"Default": "Information"
},
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "Sentry"
}
],
"Enrich": [
"FromLogContext"
],
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "@x is not null and TypeOf(@x) not like '%OperationCanceledException%'"
}
}
]
},
"HttpLogging": {
"LoggingFields": "None"
},
"AllowedHosts": "*"
}
31 changes: 26 additions & 5 deletions src/QuickstartTemplate.WebApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
"Serilog": {
"MinimumLevel": {
"Default": "Information"
},
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "Sentry"
}
],
"Enrich": [
"FromLogContext"
],
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "@x is not null and TypeOf(@x) not like '%OperationCanceledException%'"
}
}
]
},
"HttpLogging": {
"LoggingFields": "None"
},
"AllowedHosts": "*"
}

0 comments on commit c8d1c77

Please sign in to comment.