internal static void AddDiagnosticContext(RequestLoggingOptions options, IConfiguration config) { options.EnrichDiagnosticContext = async(diagnosticContext, httpContext) => { try { if (httpContext == null) { return; } diagnosticContext.Set("RequestBody", await ReadRequestBody(httpContext.Request)); diagnosticContext.Set("Path", httpContext.Request?.Path ?? ""); diagnosticContext.Set("QueryString", httpContext.Request?.QueryString); var includeRequestHeaders = config["Serilog:RequestLoggingOptions:IncludeRequestHeaders"]; if (string.IsNullOrWhiteSpace(includeRequestHeaders) || includeRequestHeaders.ToLower() == "true") { diagnosticContext.Set("RequestHeaders", FormatHeader(httpContext.Request?.Headers, config)); } } catch (ObjectDisposedException) { //-- This exception cause pod to reset in Kubernetes. For now will eat the exception to avoid issue. } }; }
public RequestLogging(AppFunc next, RequestLoggingOptions options) { _next = next; _options = options; Initialize(); }
public Startup(IWebHostEnvironment env) { _envJsonFile = _envJsonFile.Replace(CommonConsts.AppSettings.EnvPlaceholder, env.EnvironmentName); _env = env; var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile(_defaultJsonFile, optional: true, reloadOnChange: true) .AddJsonFile(_envJsonFile, optional: true, reloadOnChange: true) .AddEnvironmentVariables(); if (env.IsDevelopment()) { builder.AddUserSecrets <Program>(optional: true, reloadOnChange: true); } Configuration = builder.Build(); // App settings Settings.App = Configuration.Parse <AppSettings>(nameof(AppSettings)); Settings.Jwt = Configuration.Parse <JwtSettings>(nameof(JwtSettings)); Settings.Serilog = Configuration.Parse <SerilogSettings>(nameof(Serilog)); Configuration.Bind(nameof(ApiSettings), ApiSettings.Instance); // Serilog _requestLoggingOptions = Configuration.Parse <RequestLoggingOptions>( ConfigConsts.Logging.RequestLoggingOptionsKey); // Mail _smtpOptionSection = Configuration.GetSection(nameof(SmtpOption)); }
/// <summary> /// This will affect performance. /// </summary> public static IApplicationBuilder UseRequestLogging(this IApplicationBuilder builder, Action <RequestLoggingOptions> setupAction = null) { var options = new RequestLoggingOptions(); setupAction?.Invoke(options); return(builder.UseRequestLogging(options)); }
public static void UseRequestLogging(this IAppBuilder app, RequestLoggingOptions options = null) { if (options == null) { options = new RequestLoggingOptions(); } app.Use <RequestLogging>(options); }
/// <summary> /// Configures log enrichment with noise reduction for specified endpoints /// </summary> /// <param name="opts"></param> /// <param name="noisyEndpointPatterns"></param> public static void WithPactDefaults(this RequestLoggingOptions opts, string[] noisyEndpointPatterns = null) { opts.EnrichDiagnosticContext = EnrichFromContext; opts.GetLevel = (ctx, _, ex) => ex != null ? LogEventLevel.Error : ctx.Response.StatusCode > 499 ? LogEventLevel.Error : IsNoisyEndpoint(ctx, noisyEndpointPatterns) ? LogEventLevel.Verbose : LogEventLevel.Information; }
/// <summary> /// This will affect performance. /// </summary> private static IApplicationBuilder UseRequestLogging(this IApplicationBuilder builder, RequestLoggingOptions options) { if (options == null) { options = new RequestLoggingOptions(); } if (options.Enabled) { var logger = builder.ApplicationServices.GetRequiredService <ILoggerFactory>().CreateLogger(options.LoggerName ?? typeof(RequestLoggingMiddleware).Namespace); builder.UseMiddleware <RequestLoggingMiddleware>(logger, options.MaxBodyByteCount, options.ShouldLog ?? (ctx => true), options.LogPrefix); } return(builder); }
public Startup(IWebHostEnvironment env, IConfiguration configuration) { _env = env; Configuration = configuration; _resources = new List <IDisposable>(); // App Settings Settings.Set(Configuration.Parse <AppSettings>(nameof(AppSettings))); Settings.Set(Configuration.Parse <JwtSettings>(nameof(JwtSettings))); // Serilog Settings.Set(Configuration.Parse <SerilogSettings>(nameof(Serilog))); _requestLoggingOptions = Configuration.Parse <RequestLoggingOptions>(LoggingConsts.RequestLoggingOptionsKey); // Mail _smtpOptionSection = Configuration.GetSection(nameof(SmtpOption)); }
public static void SetupRequests(RequestLoggingOptions options) { options.EnrichDiagnosticContext = (diagContext, httpContext) => { var context = httpContext .Features .Get <LoggingContext>(); if (context == null) { context = LoggingContext.Root; httpContext .Features .Set(context); } diagContext.Set(_ScopeIdPropName, context.ScopeId); diagContext.Set(_ParentScopeIdPropName, context.ParentScopeId); }; options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms ScopeId: " + "{" + _ScopeIdPropName + "} ParentScopeId: " + "{" + _ParentScopeIdPropName + "}"; }