Skip to content

Commit 75abc07

Browse files
committed
Simplified logging and added env vars to README
1 parent 02abfa8 commit 75abc07

File tree

4 files changed

+12
-70
lines changed

4 files changed

+12
-70
lines changed

Configs.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@
2222

2323
namespace DuplicatiIngress;
2424

25-
/// <summary>
26-
/// Represents the Serilog configuration
27-
/// </summary>
28-
/// <param name="SourceToken">The Source Token</param>
29-
/// <param name="Endpoint">The endpoint for the ingesting host</param>
30-
public record SerilogConfig(string SourceToken, string? Endpoint = null);
31-
3225
/// <summary>
3326
/// Represents the Messaging configuration
3427
/// </summary>

DuplicatiIngress.csproj

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="BetterStack.Logs.Serilog" Version="1.1.0" />
1110
<PackageReference Include="MassTransit.SqlTransport.PostgreSQL" Version="8.4.1" />
1211

1312
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
1413

15-
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
16-
<PackageReference Include="Serilog.Enrichers.ClientInfo" Version="2.1.2" />
17-
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
18-
1914
<PackageReference Include="KVPSButter" Version="0.0.3-beta" />
2015
<PackageReference Include="KVPSButter.S3" Version="0.0.11-alpha" />
2116
<PackageReference Include="KVPSButter.Postgres" Version="0.0.1-alpha" />
@@ -24,7 +19,8 @@
2419
<PackageReference Include="UuidExtensions" Version="1.2.0" />
2520
<PackageReference Include="RobotsTxtCore" Version="3.0.0" />
2621

27-
<PackageReference Include="SimpleSecurityFilter" Version="1.0.2" />
22+
<PackageReference Include="SimpleSecurityFilter" Version="1.0.2" />
23+
<PackageReference Include="CommonDuplicatiConsoleLogging" Version="1.0.1" />
2824
</ItemGroup>
2925

3026
</Project>

Program.cs

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
// SOFTWARE.
20+
using CommonLoggingConfig;
2021
using DuplicatiIngress;
2122
using MassTransit;
2223
using MassTransit.SqlTransport.PostgreSql;
@@ -25,33 +26,10 @@
2526
using Microsoft.IdentityModel.Tokens;
2627
using RobotsTxt;
2728
using Serilog;
28-
using Serilog.Core;
29-
using Serilog.Events;
3029
using SimpleSecurityFilter;
3130

3231
var builder = WebApplication.CreateBuilder(args);
3332

34-
var envConfig = builder.Configuration.GetRequiredSection("Environment").Get<EnvironmentConfig>()!;
35-
builder.Services.AddSingleton(envConfig);
36-
37-
// Prepare logging
38-
var envLogLevel = builder.Configuration.GetValue<string>("Serilog:MinimumLevel:Default");
39-
var logLevelSwitch = new LoggingLevelSwitch(
40-
!string.IsNullOrWhiteSpace(envLogLevel)
41-
? Enum.Parse<LogEventLevel>(envLogLevel)
42-
: LogEventLevel.Information);
43-
44-
var logConfiguration = new LoggerConfiguration()
45-
.Enrich.FromLogContext()
46-
.Enrich.WithClientIp()
47-
.Enrich.WithCorrelationId(headerName: "X-Request-Id")
48-
.Enrich.WithRequestHeader("X-Forwarded-For")
49-
.Enrich.WithRequestHeader("User-Agent")
50-
.MinimumLevel.ControlledBy(logLevelSwitch)
51-
.WriteTo.Console();
52-
53-
builder.Host.UseSerilog();
54-
5533
// Support the untracked local environment variables file for development
5634
if (builder.Environment.IsDevelopment())
5735
{
@@ -66,25 +44,14 @@
6644

6745
builder.Configuration.AddEnvironmentVariables();
6846

47+
var envConfig = builder.Configuration.GetRequiredSection("Environment").Get<EnvironmentConfig>()!;
48+
builder.Services.AddSingleton(envConfig);
49+
6950
var serilogConfig = builder.Configuration.GetSection("Serilog").Get<SerilogConfig>();
70-
if (!string.IsNullOrWhiteSpace(serilogConfig?.SourceToken))
71-
{
72-
if (string.IsNullOrWhiteSpace(serilogConfig.Endpoint))
73-
{
74-
logConfiguration = logConfiguration.WriteTo.BetterStack(
75-
sourceToken: serilogConfig.SourceToken
76-
);
77-
}
78-
else
79-
{
80-
logConfiguration = logConfiguration.WriteTo.BetterStack(
81-
sourceToken: serilogConfig.SourceToken,
82-
betterStackEndpoint: serilogConfig.Endpoint
83-
);
84-
}
85-
}
51+
var extras = new LoggingExtras() { IsProd = envConfig.IsProd, Hostname = envConfig.Hostname, MachineName = envConfig.MachineName };
52+
53+
builder.AddCommonLogging(serilogConfig, extras);
8654

87-
builder.Services.AddHttpContextAccessor();
8855

8956
var securityconfig = builder.Configuration.GetSection("Security").Get<SimpleSecurityOptions>();
9057
builder.AddSimpleSecurityFilter(securityconfig, msg => Log.Warning(msg));
@@ -116,12 +83,6 @@
11683

11784
builder.Services.AddSingleton(KVPSButter.KVPSLoader.CreateIKVPS(envConfig.Storage));
11885

119-
Log.Logger = logConfiguration
120-
.Enrich.WithProperty("Hostname", envConfig.Hostname ?? Environment.MachineName)
121-
.Enrich.WithProperty("MachineName", envConfig.MachineName)
122-
.Enrich.WithProperty("IsProd", envConfig.IsProd)
123-
.CreateLogger();
124-
12586
var preconfiguredTokenConfig = await PreconfiguredTokens.LoadFromStorage(builder.Configuration.GetSection("PreconfiguredTokens").Get<TokenRuleOverrideConfig>());
12687

12788
builder.Services
@@ -156,17 +117,7 @@
156117

157118

158119
var app = builder.Build();
159-
app.UseSerilogRequestLogging(options =>
160-
{
161-
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
162-
{
163-
var proto = httpContext.Request.Headers["X-Forwarded-Proto"].FirstOrDefault() ?? httpContext.Request.Scheme;
164-
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
165-
diagnosticContext.Set("HttpRequestType", httpContext.Request.Method);
166-
diagnosticContext.Set("HttpRequestUrl", $"{proto}://{httpContext.Request.Host}{httpContext.Request.Path}{httpContext.Request.QueryString}");
167-
diagnosticContext.Set("HttpRequestId", httpContext.TraceIdentifier);
168-
};
169-
});
120+
app.UseCommonLogging();
170121

171122
app.UseSimpleSecurityFilter(securityconfig);
172123

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ The following environment variables are optional, and should be considered for a
4747
| SECURITY\_\_MAXREQUESTSPERSECONDPERIP | The maximum number of request from a single IP per second before throttling it |
4848
| SECURITY\_\_FILTERPATTERNS | Boolean toggling filtering of scanning patterns |
4949
| SECURITY\_\_RATELIMITENABLED | Boolean toggling if IP rate limiting is enabled |
50+
| SERILOG\_\_SOURCETOKEN | The token used to log data from serilog |
51+
| SERILOG\_\_ENDPOINT | The serilog endpoint to send logs to |
5052

5153
## Setting Up Local Development Environment
5254

0 commit comments

Comments
 (0)