Skip to content

Commit

Permalink
Merge pull request #27 from nefarius/nefarius/feature/appsettings
Browse files Browse the repository at this point in the history
Added options reading from app config
  • Loading branch information
nefarius authored Sep 16, 2023
2 parents 32c0665 + e0e7e17 commit f837846
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 11 deletions.
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,48 @@ and you're all set! 👏 The `Setup` extension methods take optional configurati

## Example configurations

### Enable and customize W3C log compression
### From code

#### Enable and customize W3C log compression

The following settings use the library defaults, they're simply explained here and don't need to be exclusively set if you're satisfied with the defaults 😉

```csharp
var builder = WebApplication.CreateBuilder().Setup(options =>
{
// this will only keep three most recent uncompressed log files
options.W3C.RetainedFileCountLimit = 6;
options.W3C.RetainedFileCountLimit = 3;
// on rotation, make a compressed archive copy before deleting the original
options.W3C.CompressDeletedLogFiles = true;
// keeps the last 90 compressed log files on top of the original files
// after this, even the compressed logs are finally deleted from disk
options.W3C.RetainedCompressedFileCountLimit = 180;
options.W3C.RetainedCompressedFileCountLimit = 90;
});
```

### From `appsettings.json`

You can also alter the defaults from your configuration; simply stick to the options classes and property naming conventions like so:

```json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"WebApplicationBuilderOptions": {
"AutoDetectPrivateNetworks": false
},
"WebApplicationOptions": {
"UseForwardedHeaders": false
}
}
```

Bear in mind that changing the same option in code will take priority over application configuration.

## 3rd party credits

- [MonoMod](https://github.com/MonoMod/MonoMod)
Expand Down
6 changes: 6 additions & 0 deletions app/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"WebApplicationBuilderOptions": {
"AutoDetectPrivateNetworks": false
},
"WebApplicationOptions": {
"UseForwardedHeaders": false
}
}
4 changes: 2 additions & 2 deletions src/Options/W3CLoggingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public sealed class W3CLoggingOptions

private string _logsDirectory = Path.Combine(AppContext.BaseDirectory, "logs");

private int _retainedCompressedFileCountLimit = 180;
private int _retainedCompressedFileCountLimit = 90;

private int _retainedFileCountLimit = 6;
private int _retainedFileCountLimit = 3;

internal W3CLoggingOptions() { }

Expand Down
2 changes: 0 additions & 2 deletions src/Options/WebApplicationBuilderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ namespace Nefarius.Utilities.AspNetCore.Options;
[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Global")]
public sealed class WebApplicationBuilderOptions
{
internal WebApplicationBuilderOptions() { }

/// <summary>
/// Serilog logging options.
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions src/Options/WebApplicationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ namespace Nefarius.Utilities.AspNetCore.Options;
[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Global")]
public sealed class WebApplicationOptions
{
internal WebApplicationOptions() { }

/// <summary>
/// Use UseForwardedHeaders with KnownNetworks auto-filled.
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion src/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using Nefarius.Utilities.AspNetCore.Internal;
Expand Down Expand Up @@ -34,7 +35,11 @@ public static class WebApplicationBuilderExtensions
public static WebApplicationBuilder Setup(this WebApplicationBuilder builder,
Action<WebApplicationBuilderOptions> configure = default)
{
WebApplicationBuilderOptions options = new();
WebApplicationBuilderOptions options =
builder.Configuration
.GetSection(nameof(WebApplicationBuilderOptions))
.Get<WebApplicationBuilderOptions>()
?? new WebApplicationBuilderOptions();

configure?.Invoke(options);

Expand Down
7 changes: 6 additions & 1 deletion src/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics.CodeAnalysis;

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;

using Serilog;

Expand All @@ -20,7 +21,11 @@ public static class WebApplicationExtensions
/// </summary>
public static WebApplication Setup(this WebApplication app, Action<WebApplicationOptions> configure = default)
{
WebApplicationOptions options = new();
WebApplicationOptions options =
app.Configuration
.GetSection(nameof(WebApplicationOptions))
.Get<WebApplicationOptions>()
?? new WebApplicationOptions();

configure?.Invoke(options);

Expand Down

0 comments on commit f837846

Please sign in to comment.