MiddlewareExceptionHandler 1.0.0
See the version list below for details.
dotnet add package MiddlewareExceptionHandler --version 1.0.0
NuGet\Install-Package MiddlewareExceptionHandler -Version 1.0.0
<PackageReference Include="MiddlewareExceptionHandler" Version="1.0.0" />
paket add MiddlewareExceptionHandler --version 1.0.0
#r "nuget: MiddlewareExceptionHandler, 1.0.0"
// Install MiddlewareExceptionHandler as a Cake Addin
#addin nuget:?package=MiddlewareExceptionHandler&version=1.0.0
// Install MiddlewareExceptionHandler as a Cake Tool
#tool nuget:?package=MiddlewareExceptionHandler&version=1.0.0
#Configurazioni iniziali
##In Program.cs
//Add middleware service
builder.Services.AddScoped<GlobalExceptionHandlingMiddleware>();
//Add Serilog
builder.Host.UseSerilog((context, configuration) ⇒ configuration.ReadFrom.Configuration(context.Configuration));
//Add Serilog middleware
app.UseSerilogRequestLogging(options ⇒ { // Customize the output template options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms";
// Exclude specific response codes from logging
options.GetLevel = (httpContext, elapsed, ex) =>
{
if (httpContext.Response.StatusCode >= StatusCodes.Status400BadRequest)
{
return LogEventLevel.Error;
}
return LogEventLevel.Information;
};
// Customize the request completion event
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
{
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
};
});
//Add
app.UseMiddleware<GlobalExceptionHandlingMiddleware>();
##In Controllers:
Cambiare ereditarietà dei controller con : BaseApiController e iniettare ILogger<TuoController> _logger;
public TuoController(ILogger<TuoController> logger) : base(logger)
{
_logger = logger;
}
Utilizzare StandardMessageResult per gestire le risposte delle tue api methods:
return StandardMessageResult(HttpStatusCode.OK, message: "Company trovata", result: result);
##In appsettings.json
"Serilog": { "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], "MinimumLevel": { "Default": "Information", "Override": { "Microsoft": "Warning", "System": "Warning" } }, "WriteTo": [ { "Name": "Console" }, { "Name": "File", "Args": { "path": "Logs/log-.txt", "rollingInterval": "Day", "rollOnFileSizeLimit": true, "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact" } } ], "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], "Properties": { "Application": "WebApi", "Environment": "Production" } },
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- Serilog.AspNetCore (>= 8.0.1)
- Swashbuckle.AspNetCore (>= 6.4.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.