Release v1.0.0 #16
.envDbInitializer.csDependencyInjection.cs
.gitea/workflows
ApiDto
ApiDto.csproj
Backend.slnCommon
Requests
Responses
Endpoint
Common
Attributes
BadRequestResponseAttribute.csLocalhostAttribute.csMaintenanceModeIgnoreAttribute.csNotFoundResponseAttribute.csTokenAuthenticationAttribute.cs
Exceptions
Interfaces
Model
Services
Configuration
AppConfig
EnvironmentManager.csGeneral
Swagger
Controllers
BaseController.cs
Endpoint.csprojConfiguration
V1
CampusController.csDisciplineController.csFacultyController.csGroupController.csLectureHallController.csProfessorController.csScheduleController.cs
WeatherForecastController.csMiddleware
Program.csWeatherForecast.csSecurity
SqlData
Application
Application.csprojDependencyInjection.cs
Common
Cqrs
Campus
Queries
Discipline
Queries
Faculty
Queries
Group
Queries
LectureHall
Queries
Professor
Queries
Schedule
Interfaces
Domain
Domain.csproj
Schedule
Migrations
MysqlMigrations
Migrations
20240601023106_InitialMigration.Designer.cs20240601023106_InitialMigration.csUberDbContextModelSnapshot.cs
MysqlMigrations.csprojPsqlMigrations
Migrations
20240601021702_InitialMigration.Designer.cs20240601021702_InitialMigration.csUberDbContextModelSnapshot.cs
PsqlMigrations.csprojSqliteMigrations
Persistence
Common
BaseDbContext.csConfigurationResolver.csDatabaseProvider.csDbContextFactory.csModelBuilderExtensions.cs
Contexts
Schedule
EntityTypeConfigurations
Persistence.csprojUberDbContext.cs
79
Endpoint/Configuration/AppConfig/LoggerConfiguration.cs
Normal file
79
Endpoint/Configuration/AppConfig/LoggerConfiguration.cs
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Mirea.Api.Endpoint.Common.Services;
|
||||||
|
using Mirea.Api.Endpoint.Configuration.General;
|
||||||
|
using Serilog;
|
||||||
|
using Serilog.Events;
|
||||||
|
using Serilog.Filters;
|
||||||
|
using Serilog.Formatting.Compact;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Mirea.Api.Endpoint.Configuration.AppConfig;
|
||||||
|
|
||||||
|
public static class LoggerConfiguration
|
||||||
|
{
|
||||||
|
public static IHostBuilder AddCustomSerilog(this IHostBuilder hostBuilder)
|
||||||
|
{
|
||||||
|
hostBuilder.UseSerilog((context, _, configuration) =>
|
||||||
|
{
|
||||||
|
var generalConfig = context.Configuration.Get<GeneralConfig>();
|
||||||
|
configuration
|
||||||
|
.MinimumLevel.Debug()
|
||||||
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
||||||
|
.Enrich.FromLogContext()
|
||||||
|
.WriteTo.Console(
|
||||||
|
outputTemplate:
|
||||||
|
"[{Level:u3}] [{Timestamp:dd.MM.yyyy HH:mm:ss}] {Message:lj}{NewLine}{Exception}");
|
||||||
|
|
||||||
|
if (generalConfig?.LogSettings?.EnableLogToFile == true)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(generalConfig.LogSettings.LogFilePath) && Directory.Exists(PathBuilder.Combine(generalConfig.LogSettings.LogFilePath)))
|
||||||
|
Directory.CreateDirectory(generalConfig.LogSettings.LogFilePath);
|
||||||
|
|
||||||
|
configuration.WriteTo.File(
|
||||||
|
new CompactJsonFormatter(),
|
||||||
|
PathBuilder.Combine(
|
||||||
|
generalConfig.LogSettings.LogFilePath!,
|
||||||
|
generalConfig.LogSettings.LogFileName + ".json"
|
||||||
|
),
|
||||||
|
LogEventLevel.Debug,
|
||||||
|
rollingInterval: RollingInterval.Day);
|
||||||
|
}
|
||||||
|
|
||||||
|
configuration
|
||||||
|
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning)
|
||||||
|
.MinimumLevel.Override("Microsoft.AspNetCore.Mvc", LogEventLevel.Warning)
|
||||||
|
.MinimumLevel.Override("Microsoft.AspNetCore.Routing", LogEventLevel.Warning);
|
||||||
|
|
||||||
|
configuration.Filter.ByExcluding(Matching.WithProperty<string>("SourceContext", sc =>
|
||||||
|
sc.Contains("Microsoft.EntityFrameworkCore.Database.Command")));
|
||||||
|
});
|
||||||
|
|
||||||
|
return hostBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IApplicationBuilder UseCustomSerilog(this IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseSerilogRequestLogging(options =>
|
||||||
|
{
|
||||||
|
options.MessageTemplate = "Handled {RequestPath} in {Elapsed:0.0000} ms";
|
||||||
|
|
||||||
|
options.GetLevel = (_, elapsed, ex) => elapsed >= 2500 || ex != null
|
||||||
|
? LogEventLevel.Warning
|
||||||
|
: elapsed >= 1000
|
||||||
|
? LogEventLevel.Information
|
||||||
|
: LogEventLevel.Debug;
|
||||||
|
|
||||||
|
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
|
||||||
|
{
|
||||||
|
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
|
||||||
|
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
|
||||||
|
diagnosticContext.Set("UserAgent", httpContext.Request.Headers.UserAgent);
|
||||||
|
diagnosticContext.Set("RemoteIPAddress", httpContext.Connection.RemoteIpAddress?.ToString());
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
}
|
@@ -25,6 +25,10 @@
|
|||||||
<PackageReference Include="Cronos" Version="0.8.4" />
|
<PackageReference Include="Cronos" Version="0.8.4" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
|
||||||
<PackageReference Include="StackExchange.Redis" Version="2.7.33" />
|
<PackageReference Include="StackExchange.Redis" Version="2.7.33" />
|
||||||
|
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
|
||||||
|
<PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Versioning" Version="2.0.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Versioning" Version="2.0.0" />
|
||||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.0" />
|
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
Reference in New Issue
Block a user