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 if (applicationBuilder ==
null)
41 throw new ArgumentNullException(nameof(applicationBuilder));
43 applicationBuilder.Use(async (context, next) =>
50 catch (DbUpdateException e)
52 if (e.InnerException is OperationCanceledException)
54 logger.LogTrace(e,
"Rethrowing DbUpdateException as OperationCanceledException");
55 throw e.InnerException;
58 logger.LogDebug(e,
"Database conflict!");
61 AdditionalData = String.Format(CultureInfo.InvariantCulture, (e.InnerException ?? e).Message),
62 }).ExecuteResultAsync(
new ActionContext
64 HttpContext = context,
76 if (applicationBuilder ==
null)
77 throw new ArgumentNullException(nameof(applicationBuilder));
78 applicationBuilder.Use(async (context, next) =>
80 context.Response.Headers.Add(HeaderNames.CacheControl,
new StringValues(
"no-cache"));
91 if (applicationBuilder ==
null)
92 throw new ArgumentNullException(nameof(applicationBuilder));
93 applicationBuilder.Use(async (context, next) =>
100 catch (OperationCanceledException ex)
102 logger.LogDebug(ex,
"Request cancelled!");
113 if (applicationBuilder ==
null)
114 throw new ArgumentNullException(nameof(applicationBuilder));
116 applicationBuilder.Use(async (context, next) =>
125 logger.LogError(e,
"Failed request!");
126 await
new JsonResult(
129 AdditionalData = e.ToString(),
132 StatusCode = (int)HttpStatusCode.InternalServerError,
134 .ExecuteResultAsync(
new ActionContext
136 HttpContext = context,
149 if (applicationBuilder ==
null)
150 throw new ArgumentNullException(nameof(applicationBuilder));
151 if (assemblyInformationProvider ==
null)
152 throw new ArgumentNullException(nameof(assemblyInformationProvider));
154 applicationBuilder.Use(async (context, next) =>
156 context.Response.Headers.Add(
"X-Powered-By", assemblyInformationProvider.
VersionPrefix);
168 if (applicationBuilder ==
null)
169 throw new ArgumentNullException(nameof(applicationBuilder));
170 if (swarmConfiguration ==
null)
171 throw new ArgumentNullException(nameof(swarmConfiguration));
173 if (LogSwarmIdentifier && swarmConfiguration.
Identifier !=
null)
174 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.