2using System.Globalization;
5using Microsoft.AspNetCore.Builder;
6using Microsoft.AspNetCore.Http;
7using Microsoft.AspNetCore.Mvc;
8using Microsoft.EntityFrameworkCore;
9using Microsoft.Extensions.DependencyInjection;
10using Microsoft.Extensions.Logging;
11using Microsoft.Extensions.Primitives;
12using Microsoft.Net.Http.Headers;
32 internal static bool LogSwarmIdentifier {
get;
set; }
40 ArgumentNullException.ThrowIfNull(applicationBuilder);
42 applicationBuilder.Use(async (context, next) =>
49 catch (DbUpdateException e)
51 if (e.InnerException is OperationCanceledException)
53 logger.LogTrace(e,
"Rethrowing DbUpdateException as OperationCanceledException");
54 throw e.InnerException;
57 logger.LogDebug(e,
"Database conflict!");
60 AdditionalData = String.Format(CultureInfo.InvariantCulture, (e.InnerException ?? e).Message),
61 }).ExecuteResultAsync(
new ActionContext
63 HttpContext = context,
75 ArgumentNullException.ThrowIfNull(applicationBuilder);
76 applicationBuilder.Use(async (context, next) =>
78 context.Response.Headers.Add(HeaderNames.CacheControl,
new StringValues(
"no-cache"));
89 ArgumentNullException.ThrowIfNull(applicationBuilder);
90 applicationBuilder.Use(async (context, next) =>
97 catch (OperationCanceledException ex)
99 logger.LogDebug(ex,
"Request cancelled!");
110 ArgumentNullException.ThrowIfNull(applicationBuilder);
112 applicationBuilder.Use(async (context, next) =>
121 logger.LogError(e,
"Failed request!");
122 await
new JsonResult(
125 AdditionalData = e.ToString(),
128 StatusCode = (int)HttpStatusCode.InternalServerError,
130 .ExecuteResultAsync(
new ActionContext
132 HttpContext = context,
145 ArgumentNullException.ThrowIfNull(applicationBuilder);
146 ArgumentNullException.ThrowIfNull(assemblyInformationProvider);
148 applicationBuilder.Use(async (context, next) =>
150 context.Response.Headers.Add(
"X-Powered-By", assemblyInformationProvider.
VersionPrefix);
162 ArgumentNullException.ThrowIfNull(applicationBuilder);
163 ArgumentNullException.ThrowIfNull(swarmConfiguration);
165 if (LogSwarmIdentifier && swarmConfiguration.
Identifier !=
null)
166 applicationBuilder.Use(async (context, next) =>
string? Identifier
The server's identifier.
Represents an error message returned by the server.
Configuration for the server swarm system.
Extensions for IApplicationBuilder.
static void UseServerBranding(this IApplicationBuilder applicationBuilder, IAssemblyInformationProvider assemblyInformationProvider)
Add the X-Powered-By response header.
static void UseDisabledClientCache(this IApplicationBuilder applicationBuilder)
Suppress any client side caching of API calls.
static void UseServerErrorHandling(this IApplicationBuilder applicationBuilder)
Suppress all in flight Exceptions for the request with error 500.
static ILogger GetLogger(HttpContext httpContext)
Gets a ILogger from a given httpContext .
static void UseDbConflictHandling(this IApplicationBuilder applicationBuilder)
Return a ConflictObjectResult for DbUpdateExceptions.
static void UseCancelledRequestSuppression(this IApplicationBuilder applicationBuilder)
Suppress global::System.Threading.Tasks.TaskCanceledException warnings when a user aborts a request.
static void UseAdditionalRequestLoggingContext(this IApplicationBuilder applicationBuilder, SwarmConfiguration swarmConfiguration)
Adds additional global LogContext to the request pipeline.
Helpers for manipulating the Serilog.Context.LogContext.
const string SwarmIdentifierContextProperty
The Serilog.Context.LogContext property name for Api.Models.Internal.SwarmServer.Identifiers.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.