| | | 1 | | using ArturRios.Data.Relational.Core.DependencyInjection; |
| | | 2 | | using ArturRios.Heimdall.Shared.Retention; |
| | | 3 | | using ArturRios.Heimdall.WebApi.Security; |
| | | 4 | | using ArturRios.Messaging.Email; |
| | | 5 | | using ArturRios.Util.WebApi.Configuration; |
| | | 6 | | using ArturRios.Util.WebApi.Security.Extensions; |
| | | 7 | | using Serilog; |
| | | 8 | | using Serilog.Events; |
| | | 9 | | using Serilog.Formatting.Json; |
| | | 10 | | |
| | | 11 | | namespace ArturRios.Heimdall.WebApi; |
| | | 12 | | |
| | | 13 | | internal static class LoggingConfiguration |
| | | 14 | | { |
| | | 15 | | private const string LogDirectoryEnvironmentVariable = "HEIMDALL_LOG_DIRECTORY"; |
| | | 16 | | private const string DefaultLogDirectory = "logs"; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Configures the console and file sinks, and bounds how long the files live (NFR-22). |
| | | 20 | | /// </summary> |
| | | 21 | | /// <remarks> |
| | | 22 | | /// <para> |
| | | 23 | | /// <b>The year/month directory layout is gone, deliberately.</b> The sink used to be |
| | | 24 | | /// wrapped in <c>WriteTo.Map</c> keyed on <c>yyyy/MM</c>, which created a <em>new sink |
| | | 25 | | /// per month</em>. A retention limit bounds the files within one sink, so it would have |
| | | 26 | | /// bounded each month's directory and never removed a month — the files from the first |
| | | 27 | | /// month a deployment ran were still on disk years later. A flat rolling sink with |
| | | 28 | | /// <c>retainedFileTimeLimit</c> is the shape where the limit actually applies. |
| | | 29 | | /// </para> |
| | | 30 | | /// <para> |
| | | 31 | | /// Read straight from the environment rather than through the configured options, |
| | | 32 | | /// because this runs before <c>LoadConfiguration</c> — the same reason the log directory |
| | | 33 | | /// is read this way. An unusable value falls back to the documented default, silently |
| | | 34 | | /// here because there is no logger yet to warn with; <c>AddDataRetention</c> reads the |
| | | 35 | | /// same variable later and does warn. |
| | | 36 | | /// </para> |
| | | 37 | | /// <para> |
| | | 38 | | /// <b>Every request's client IP address is logged</b>, by Util.WebApi's |
| | | 39 | | /// <c>TraceActivityMiddleware</c>, as the address the request came from — the real caller's |
| | | 40 | | /// once <see cref="TrustedProxyOptions" /> names the proxy in front of the API. It is |
| | | 41 | | /// security telemetry, kept for the same period as the rest of the log, and the Data |
| | | 42 | | /// Retention Schedule and the Privacy Notice say so. |
| | | 43 | | /// </para> |
| | | 44 | | /// <para> |
| | | 45 | | /// <b><c>MailgunEmailService</c> is silenced.</b> The messaging library logs every send |
| | | 46 | | /// with the recipient's address in the clear — "Sending e-mail to …" and "accepted the |
| | | 47 | | /// message for …" at Information, the same on a rejection at Error — which is exactly what |
| | | 48 | | /// NFR-22 forbids, and this codebase's own redaction cannot reach a library's templates. |
| | | 49 | | /// Nothing is lost: <c>MailgunSender</c> already logs every outcome, refusal and exception |
| | | 50 | | /// included, under a <c>LogSafeEmail</c> reference. |
| | | 51 | | /// </para> |
| | | 52 | | /// </remarks> |
| | | 53 | | internal static void Configure() |
| | | 54 | | { |
| | 973 | 55 | | var logDirectory = Environment.GetEnvironmentVariable(LogDirectoryEnvironmentVariable) |
| | 973 | 56 | | ?? DefaultLogDirectory; |
| | | 57 | | |
| | 973 | 58 | | var retention = DataRetentionOptions.FromEnvironment().LogRetention; |
| | | 59 | | |
| | 973 | 60 | | Log.Logger = new LoggerConfiguration() |
| | 973 | 61 | | .MinimumLevel.Override(typeof(MailgunEmailService).FullName!, LogEventLevel.Fatal) |
| | 973 | 62 | | .WriteTo.Console(new JsonFormatter()) |
| | 973 | 63 | | .WriteTo.File( |
| | 973 | 64 | | new JsonFormatter(), |
| | 973 | 65 | | Path.Combine(logDirectory, "log-.json"), |
| | 973 | 66 | | rollingInterval: RollingInterval.Day, |
| | 973 | 67 | | retainedFileTimeLimit: retention) |
| | 973 | 68 | | .CreateLogger(); |
| | 973 | 69 | | } |
| | | 70 | | } |