1
0
mirror of synced 2024-11-23 22:41:01 +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]
[Route("api/[controller]")]

View File

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

View File

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

View File

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

View File

@ -1,5 +1,4 @@
using SharedProject.Models.Responses;
using TaikoLocalServer.Services.Interfaces;
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.Responses;
using SharedProject.Utils;
using TaikoLocalServer.Services;
using TaikoLocalServer.Services.Interfaces;
using Throw;
namespace TaikoLocalServer.Controllers.Api;

View File

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

View File

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

View File

@ -1,5 +1,4 @@
using TaikoLocalServer.Services.Interfaces;
using Throw;
using Throw;
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")]
[ApiController]

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -9,4 +9,6 @@ global using TaikoLocalServer.Common;
global using TaikoLocalServer.Common.Utils;
global using TaikoLocalServer.Context;
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 Microsoft.AspNetCore.HttpLogging;
using Microsoft.AspNetCore.HttpOverrides;
using TaikoLocalServer.Middlewares;
using TaikoLocalServer.Services;
using TaikoLocalServer.Services.Extentions;
using TaikoLocalServer.Services.Interfaces;
using TaikoLocalServer.Settings;
using Throw;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
// Manually enable tls 1.0
builder.WebHost.UseKestrel(kestrelOptions =>
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
var version = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
.InformationalVersion;
Log.Information("TaikoLocalServer version {Version}", version);
Log.Information("Server starting up...");
try
{
kestrelOptions.ConfigureHttpsDefaults(httpsOptions =>
var builder = WebApplication.CreateBuilder(args);
// Manually enable tls 1.0
builder.WebHost.UseKestrel(kestrelOptions =>
{
httpsOptions.SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13;
kestrelOptions.ConfigureHttpsDefaults(httpsOptions =>
{
httpsOptions.SslProtocols =
SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13;
});
});
});
// 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"];
if (string.IsNullOrEmpty(dbName))
builder.Host.UseSerilog((context, configuration) =>
{
dbName = Constants.DEFAULT_DB_NAME;
configuration.WriteTo.Console().ReadFrom.Configuration(context.Configuration);
});
// 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"];
if (string.IsNullOrEmpty(dbName))
{
dbName = Constants.DEFAULT_DB_NAME;
}
var path = Path.Combine(PathHelper.GetRootPath(), dbName);
option.UseSqlite($"Data Source={path}");
});
builder.Services.AddMemoryCache();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAllCorsPolicy", policy =>
{
policy
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
builder.Services.AddTaikoDbServices();
var app = builder.Build();
// Migrate db
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<TaikoDbContext>();
db.Database.Migrate();
}
var path = Path.Combine(PathHelper.GetRootPath(), dbName);
option.UseSqlite($"Data Source={path}");
});
builder.Services.AddHttpLogging(options =>
{
options.LoggingFields = HttpLoggingFields.RequestProperties |
HttpLoggingFields.ResponseStatusCode;
});
builder.Services.AddMemoryCache();
builder.Services.AddCors(options =>
{
options.AddPolicy("DevCorsPolicy", policy =>
app.UseSerilogRequestLogging(options =>
{
policy
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
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);
};
});
});
builder.Services.AddTaikoDbServices();
var app = builder.Build();
var gameDataService = app.Services.GetService<IGameDataService>();
gameDataService.ThrowIfNull();
await gameDataService.InitializeAsync();
// Migrate db
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<TaikoDbContext>();
db.Database.Migrate();
// For reverse proxy
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseCors("AllowAllCorsPolicy");
// For blazor hosting
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseHttpLogging();
app.MapControllers();
app.MapFallbackToFile("index.html");
app.UseWhen(
context => context.Request.Path.StartsWithSegments("/sys/servlet/PowerOn", StringComparison.InvariantCulture),
applicationBuilder => applicationBuilder.UseAllNetRequestMiddleware());
app.Run();
}
var gameDataService = app.Services.GetService<IGameDataService>();
gameDataService.ThrowIfNull();
await gameDataService.InitializeAsync();
// For reverse proxy
app.UseForwardedHeaders(new ForwardedHeadersOptions
catch (Exception ex)
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseCors("DevCorsPolicy");
// For blazor hosting
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseHttpLogging();
app.MapControllers();
app.MapFallbackToFile("index.html");
app.UseWhen(context => context.Request.Path.StartsWithSegments("/sys/servlet/PowerOn", StringComparison.InvariantCulture),
applicationBuilder => applicationBuilder.UseAllNetRequestMiddleware());
app.Run();
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;

View File

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

View File

@ -1,6 +1,4 @@
using TaikoLocalServer.Services.Interfaces;
namespace TaikoLocalServer.Services;
namespace TaikoLocalServer.Services;
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
{

View File

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

View File

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

View File

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

View File

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

View File

@ -4,6 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>0.3.0-alpha</Version>
</PropertyGroup>
<ItemGroup>
@ -20,6 +21,8 @@
</PackageReference>
<PackageReference Include="protobuf-net" 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="Swan.Core" Version="6.0.2-beta.90" />
<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",
"GameUrl": "vsapi.taiko-p.jp"
},
"DbFileName" : "taiko.db3",
"Logging": {
"LogLevel": {
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
}
"Override": {
"Microsoft": "Warning",
"Microsoft.AspNetCore": "Warning",
"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": "*",
"Kestrel": {
"Endpoints": {
"Server": {