1
0
mirror of synced 2024-11-24 06:50:15 +01:00

Use serilog for logging (support log to file)

Clean up
This commit is contained in:
asesidaa 2022-09-19 01:18:24 +08:00
parent 2de8d8ab86
commit 45947f0492
32 changed files with 155 additions and 147 deletions

View File

@ -1,6 +1,4 @@
using TaikoLocalServer.Services.Interfaces; namespace TaikoLocalServer.Controllers.Api;
namespace TaikoLocalServer.Controllers.Api;
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/[controller]")]

View File

@ -1,7 +1,6 @@
using SharedProject.Models; using SharedProject.Models;
using SharedProject.Models.Responses; using SharedProject.Models.Responses;
using Swan.Mapping; using Swan.Mapping;
using TaikoLocalServer.Services.Interfaces;
namespace TaikoLocalServer.Controllers.Api; namespace TaikoLocalServer.Controllers.Api;

View File

@ -1,7 +1,4 @@
using SharedProject.Models; using SharedProject.Models.Responses;
using SharedProject.Models.Responses;
using Swan.Mapping;
using TaikoLocalServer.Services.Interfaces;
namespace TaikoLocalServer.Controllers.Api; namespace TaikoLocalServer.Controllers.Api;

View File

@ -1,5 +1,4 @@
using SharedProject.Models.Requests; using SharedProject.Models.Requests;
using TaikoLocalServer.Services.Interfaces;
namespace TaikoLocalServer.Controllers.Api; namespace TaikoLocalServer.Controllers.Api;

View File

@ -1,5 +1,4 @@
using SharedProject.Models.Responses; using SharedProject.Models.Responses;
using TaikoLocalServer.Services.Interfaces;
namespace TaikoLocalServer.Controllers.Api; namespace TaikoLocalServer.Controllers.Api;

View File

@ -1,11 +1,6 @@
using System.Buffers.Binary; using System.Text.Json;
using System.Text.Json;
using SharedProject.Models; using SharedProject.Models;
using SharedProject.Models.Responses;
using SharedProject.Utils; using SharedProject.Utils;
using TaikoLocalServer.Services;
using TaikoLocalServer.Services.Interfaces;
using Throw;
namespace TaikoLocalServer.Controllers.Api; namespace TaikoLocalServer.Controllers.Api;

View File

@ -1,5 +1,4 @@
using System.Text.Json; using System.Text.Json;
using TaikoLocalServer.Services.Interfaces;
using Throw; using Throw;
namespace TaikoLocalServer.Controllers.Game; namespace TaikoLocalServer.Controllers.Game;
@ -164,7 +163,7 @@ public class BaidController : BaseController<BaidController>
IsDispAchievementTypeSet = true, IsDispAchievementTypeSet = true,
LastPlayMode = userData.LastPlayMode, LastPlayMode = userData.LastPlayMode,
IsDispSouuchiOn = true, IsDispSouuchiOn = true,
AiRank = 0, AiRank = 1,
AiTotalWin = (uint)totalWin, AiTotalWin = (uint)totalWin,
Accesstoken = "123456", Accesstoken = "123456",
ContentInfo = GZipBytesUtil.GetGZipBytes(new byte[10]) ContentInfo = GZipBytesUtil.GetGZipBytes(new byte[10])

View File

@ -1,6 +1,4 @@
using TaikoLocalServer.Services.Interfaces; namespace TaikoLocalServer.Controllers.Game;
namespace TaikoLocalServer.Controllers.Game;
[Route("/v12r03/chassis/crownsdata.php")] [Route("/v12r03/chassis/crownsdata.php")]
[ApiController] [ApiController]

View File

@ -1,6 +1,4 @@
using TaikoLocalServer.Services.Interfaces; namespace TaikoLocalServer.Controllers.Game;
namespace TaikoLocalServer.Controllers.Game;
[Route("/v12r03/chassis/getaidata.php")] [Route("/v12r03/chassis/getaidata.php")]
[ApiController] [ApiController]
@ -24,7 +22,9 @@ public class GetAiDataController : BaseController<GetAiDataController>
var response = new GetAiDataResponse var response = new GetAiDataResponse
{ {
Result = 1, Result = 1,
TotalWinnings = (uint)totalWin TotalWinnings = (uint)totalWin,
InputMedian = "1000",
InputVariance = "2000"
}; };
return Ok(response); return Ok(response);

View File

@ -1,5 +1,4 @@
using TaikoLocalServer.Services.Interfaces; using Throw;
using Throw;
namespace TaikoLocalServer.Controllers.Game; namespace TaikoLocalServer.Controllers.Game;

View File

@ -1,6 +1,4 @@
using TaikoLocalServer.Services.Interfaces; namespace TaikoLocalServer.Controllers.Game;
namespace TaikoLocalServer.Controllers.Game;
[Route("/v12r03/chassis/getdanodai.php")] [Route("/v12r03/chassis/getdanodai.php")]
[ApiController] [ApiController]

View File

@ -1,6 +1,4 @@
using TaikoLocalServer.Services.Interfaces; namespace TaikoLocalServer.Controllers.Game;
namespace TaikoLocalServer.Controllers.Game;
[Route("/v12r03/chassis/getdanscore.php")] [Route("/v12r03/chassis/getdanscore.php")]
[ApiController] [ApiController]

View File

@ -1,6 +1,4 @@
using TaikoLocalServer.Services.Interfaces; namespace TaikoLocalServer.Controllers.Game;
namespace TaikoLocalServer.Controllers.Game;
[Route("/v12r03/chassis/getscorerank.php")] [Route("/v12r03/chassis/getscorerank.php")]
[ApiController] [ApiController]

View File

@ -1,6 +1,4 @@
using TaikoLocalServer.Services.Interfaces; namespace TaikoLocalServer.Controllers.Game;
namespace TaikoLocalServer.Controllers.Game;
[Route("/v12r03/chassis/getsongintroduction.php")] [Route("/v12r03/chassis/getsongintroduction.php")]
[ApiController] [ApiController]

View File

@ -1,6 +1,4 @@
using TaikoLocalServer.Services.Interfaces; namespace TaikoLocalServer.Controllers.Game;
namespace TaikoLocalServer.Controllers.Game;
[ApiController] [ApiController]
[Route("/v12r03/chassis/initialdatacheck.php")] [Route("/v12r03/chassis/initialdatacheck.php")]

View File

@ -1,7 +1,4 @@
using TaikoLocalServer.Services; namespace TaikoLocalServer.Controllers.Game;
using TaikoLocalServer.Services.Interfaces;
namespace TaikoLocalServer.Controllers.Game;
[Route("/v12r03/chassis/mydonentry.php")] [Route("/v12r03/chassis/mydonentry.php")]
[ApiController] [ApiController]

View File

@ -1,7 +1,6 @@
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Globalization; using System.Globalization;
using System.Text.Json; using System.Text.Json;
using TaikoLocalServer.Services.Interfaces;
using Throw; using Throw;
namespace TaikoLocalServer.Controllers.Game; namespace TaikoLocalServer.Controllers.Game;

View File

@ -1,5 +1,4 @@
using TaikoLocalServer.Services.Interfaces; using Throw;
using Throw;
namespace TaikoLocalServer.Controllers.Game; namespace TaikoLocalServer.Controllers.Game;

View File

@ -1,6 +1,5 @@
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Text.Json; using System.Text.Json;
using TaikoLocalServer.Services.Interfaces;
using Throw; using Throw;
namespace TaikoLocalServer.Controllers.Game; namespace TaikoLocalServer.Controllers.Game;

View File

@ -10,3 +10,5 @@ global using TaikoLocalServer.Common.Utils;
global using TaikoLocalServer.Context; global using TaikoLocalServer.Context;
global using TaikoLocalServer.Entities; global using TaikoLocalServer.Entities;
global using TaikoLocalServer.Models; global using TaikoLocalServer.Models;
global using TaikoLocalServer.Services;
global using TaikoLocalServer.Services.Interfaces;

View File

@ -1,87 +1,121 @@
using System.Reflection;
using System.Security.Authentication; using System.Security.Authentication;
using Microsoft.AspNetCore.HttpLogging;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using TaikoLocalServer.Middlewares; using TaikoLocalServer.Middlewares;
using TaikoLocalServer.Services;
using TaikoLocalServer.Services.Extentions; using TaikoLocalServer.Services.Extentions;
using TaikoLocalServer.Services.Interfaces;
using TaikoLocalServer.Settings; using TaikoLocalServer.Settings;
using Throw; using Throw;
using Serilog;
var builder = WebApplication.CreateBuilder(args); Log.Logger = new LoggerConfiguration()
// Manually enable tls 1.0 .WriteTo.Console()
builder.WebHost.UseKestrel(kestrelOptions => .CreateBootstrapLogger();
var version = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
.InformationalVersion;
Log.Information("TaikoLocalServer version {Version}", version);
Log.Information("Server starting up...");
try
{ {
var builder = WebApplication.CreateBuilder(args);
// Manually enable tls 1.0
builder.WebHost.UseKestrel(kestrelOptions =>
{
kestrelOptions.ConfigureHttpsDefaults(httpsOptions => kestrelOptions.ConfigureHttpsDefaults(httpsOptions =>
{ {
httpsOptions.SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13; httpsOptions.SslProtocols =
SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13;
});
}); });
});
// Add services to the container. builder.Host.UseSerilog((context, configuration) =>
builder.Services.AddOptions(); {
builder.Services.AddSingleton<IGameDataService, GameDataService>(); configuration.WriteTo.Console().ReadFrom.Configuration(context.Configuration);
builder.Services.Configure<UrlSettings>(builder.Configuration.GetSection(nameof(UrlSettings))); });
builder.Services.AddControllers().AddProtoBufNet();
builder.Services.AddDbContext<TaikoDbContext>(option => // Add services to the container.
{ builder.Services.AddOptions();
builder.Services.AddSingleton<IGameDataService, GameDataService>();
builder.Services.Configure<UrlSettings>(builder.Configuration.GetSection(nameof(UrlSettings)));
builder.Services.AddControllers().AddProtoBufNet();
builder.Services.AddDbContext<TaikoDbContext>(option =>
{
var dbName = builder.Configuration["DbFileName"]; var dbName = builder.Configuration["DbFileName"];
if (string.IsNullOrEmpty(dbName)) if (string.IsNullOrEmpty(dbName))
{ {
dbName = Constants.DEFAULT_DB_NAME; dbName = Constants.DEFAULT_DB_NAME;
} }
var path = Path.Combine(PathHelper.GetRootPath(), dbName); var path = Path.Combine(PathHelper.GetRootPath(), dbName);
option.UseSqlite($"Data Source={path}"); option.UseSqlite($"Data Source={path}");
}); });
builder.Services.AddHttpLogging(options => builder.Services.AddMemoryCache();
{ builder.Services.AddCors(options =>
options.LoggingFields = HttpLoggingFields.RequestProperties | {
HttpLoggingFields.ResponseStatusCode; options.AddPolicy("AllowAllCorsPolicy", policy =>
});
builder.Services.AddMemoryCache();
builder.Services.AddCors(options =>
{
options.AddPolicy("DevCorsPolicy", policy =>
{ {
policy policy
.AllowAnyOrigin() .AllowAnyOrigin()
.AllowAnyMethod() .AllowAnyMethod()
.AllowAnyHeader(); .AllowAnyHeader();
}); });
}); });
builder.Services.AddTaikoDbServices(); builder.Services.AddTaikoDbServices();
var app = builder.Build(); var app = builder.Build();
// Migrate db // Migrate db
using (var scope = app.Services.CreateScope()) using (var scope = app.Services.CreateScope())
{ {
var db = scope.ServiceProvider.GetRequiredService<TaikoDbContext>(); var db = scope.ServiceProvider.GetRequiredService<TaikoDbContext>();
db.Database.Migrate(); db.Database.Migrate();
} }
var gameDataService = app.Services.GetService<IGameDataService>(); app.UseSerilogRequestLogging(options =>
gameDataService.ThrowIfNull(); {
await gameDataService.InitializeAsync(); options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms, " +
"request host: {RequestHost}";
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
{
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
};
});
// For reverse proxy var gameDataService = app.Services.GetService<IGameDataService>();
app.UseForwardedHeaders(new ForwardedHeadersOptions gameDataService.ThrowIfNull();
{ await gameDataService.InitializeAsync();
// For reverse proxy
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
}); });
app.UseCors("DevCorsPolicy"); app.UseCors("AllowAllCorsPolicy");
// For blazor hosting // For blazor hosting
app.UseBlazorFrameworkFiles(); app.UseBlazorFrameworkFiles();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseRouting(); app.UseRouting();
app.UseHttpLogging(); app.UseHttpLogging();
app.MapControllers(); app.MapControllers();
app.MapFallbackToFile("index.html"); app.MapFallbackToFile("index.html");
app.UseWhen(context => context.Request.Path.StartsWithSegments("/sys/servlet/PowerOn", StringComparison.InvariantCulture), app.UseWhen(
context => context.Request.Path.StartsWithSegments("/sys/servlet/PowerOn", StringComparison.InvariantCulture),
applicationBuilder => applicationBuilder.UseAllNetRequestMiddleware()); applicationBuilder => applicationBuilder.UseAllNetRequestMiddleware());
app.Run(); app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Unhandled exception");
}
finally
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}

View File

@ -1,5 +1,4 @@
using TaikoLocalServer.Services.Interfaces; using Throw;
using Throw;
namespace TaikoLocalServer.Services; namespace TaikoLocalServer.Services;

View File

@ -1,6 +1,5 @@
using SharedProject.Models; using SharedProject.Models;
using Swan.Mapping; using Swan.Mapping;
using TaikoLocalServer.Services.Interfaces;
namespace TaikoLocalServer.Services; namespace TaikoLocalServer.Services;

View File

@ -1,6 +1,4 @@
using TaikoLocalServer.Services.Interfaces; namespace TaikoLocalServer.Services;
namespace TaikoLocalServer.Services;
public class DanScoreDatumService : IDanScoreDatumService public class DanScoreDatumService : IDanScoreDatumService
{ {

View File

@ -1,6 +1,4 @@
using TaikoLocalServer.Services.Interfaces; namespace TaikoLocalServer.Services.Extentions;
namespace TaikoLocalServer.Services.Extentions;
public static class ServiceExtensions public static class ServiceExtensions
{ {

View File

@ -2,7 +2,6 @@
using System.Text.Json; using System.Text.Json;
using SharedProject.Models; using SharedProject.Models;
using Swan.Mapping; using Swan.Mapping;
using TaikoLocalServer.Services.Interfaces;
using Throw; using Throw;
namespace TaikoLocalServer.Services; namespace TaikoLocalServer.Services;

View File

@ -1,6 +1,5 @@
using SharedProject.Models; using SharedProject.Models;
using Swan.Mapping; using Swan.Mapping;
using TaikoLocalServer.Services.Interfaces;
using Throw; using Throw;
namespace TaikoLocalServer.Services; namespace TaikoLocalServer.Services;

View File

@ -1,6 +1,4 @@
using TaikoLocalServer.Services.Interfaces; namespace TaikoLocalServer.Services;
namespace TaikoLocalServer.Services;
class SongPlayDatumService : ISongPlayDatumService class SongPlayDatumService : ISongPlayDatumService
{ {

View File

@ -1,5 +1,4 @@
using System.Text.Json; using System.Text.Json;
using TaikoLocalServer.Services.Interfaces;
using Throw; using Throw;
namespace TaikoLocalServer.Services; namespace TaikoLocalServer.Services;

View File

@ -4,6 +4,7 @@
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Version>0.3.0-alpha</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -20,6 +21,8 @@
</PackageReference> </PackageReference>
<PackageReference Include="protobuf-net" Version="3.1.17" /> <PackageReference Include="protobuf-net" Version="3.1.17" />
<PackageReference Include="protobuf-net.AspNetCore" Version="3.1.17" /> <PackageReference Include="protobuf-net.AspNetCore" Version="3.1.17" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0-dev-00281" />
<PackageReference Include="Serilog.Expressions" Version="3.4.1-dev-00095" />
<PackageReference Include="SharpZipLib" Version="1.3.3" /> <PackageReference Include="SharpZipLib" Version="1.3.3" />
<PackageReference Include="Swan.Core" Version="6.0.2-beta.90" /> <PackageReference Include="Swan.Core" Version="6.0.2-beta.90" />
<PackageReference Include="Swan.Logging" Version="6.0.2-beta.69" /> <PackageReference Include="Swan.Logging" Version="6.0.2-beta.69" />

View File

@ -1,8 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Information"
}
}
}

View File

@ -3,15 +3,36 @@
"MuchaUrl": "https://v402-front.mucha-prd.nbgi-amnet.jp:10122", "MuchaUrl": "https://v402-front.mucha-prd.nbgi-amnet.jp:10122",
"GameUrl": "vsapi.taiko-p.jp" "GameUrl": "vsapi.taiko-p.jp"
}, },
"DbFileName" : "taiko.db3", "DbFileName" : "taiko.db3",
"Logging": {
"LogLevel": { "Serilog": {
"MinimumLevel": {
"Default": "Information", "Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.AspNetCore": "Warning", "Microsoft.AspNetCore": "Warning",
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information" "Microsoft.Hosting.Lifetime": "Information"
} }
}, },
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "@mt = 'An unhandled exception has occurred while executing the request.'"
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": { "path": "./Logs/log-.txt", "rollingInterval": "Day" }
}
]
},
"AllowedHosts": "*", "AllowedHosts": "*",
"Kestrel": { "Kestrel": {
"Endpoints": { "Endpoints": {
"Server": { "Server": {