first commit
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
using FunGame.Server.Infrastructure.Data;
|
||||
using FunGame.Server.Infrastructure.Entities.System;
|
||||
|
||||
namespace FunGame.Server.Host.Logging;
|
||||
|
||||
/// <summary>
|
||||
/// 服务器日志通道:把 ILogger 输出(Information 及以上)写入 server_logs 表,供 WebUI 查看<para/>
|
||||
/// 宿主构建完成后通过 <see cref="Attach"/> 绑定真正的 IServiceScopeFactory(延迟绑定避免重复容器)
|
||||
/// </summary>
|
||||
public class ServerLogsLoggerProvider : ILoggerProvider
|
||||
{
|
||||
private IServiceScopeFactory? _scopeFactory;
|
||||
private readonly Lock _lock = new();
|
||||
|
||||
public void Attach(IServiceScopeFactory scopeFactory)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_scopeFactory = scopeFactory;
|
||||
}
|
||||
}
|
||||
|
||||
internal IServiceScopeFactory? GetScopeFactory()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return _scopeFactory;
|
||||
}
|
||||
}
|
||||
|
||||
public ILogger CreateLogger(string categoryName) => new ServerLogsLogger(categoryName, this);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class ServerLogsLogger(string categoryName, ServerLogsLoggerProvider provider) : ILogger
|
||||
{
|
||||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel) => logLevel >= LogLevel.Information;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
|
||||
{
|
||||
if (!IsEnabled(logLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
IServiceScopeFactory? scopeFactory = provider.GetScopeFactory();
|
||||
if (scopeFactory == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string message = formatter(state, exception);
|
||||
if (string.IsNullOrWhiteSpace(message))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (message.Length > 1024)
|
||||
{
|
||||
message = message[..1024];
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using IServiceScope scope = scopeFactory.CreateScope();
|
||||
AppDbContext db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
db.ServerLogs.Add(new ServerLog
|
||||
{
|
||||
Level = Map(logLevel),
|
||||
Source = ResolveSource(categoryName),
|
||||
Category = categoryName,
|
||||
Message = message,
|
||||
Exception = exception?.ToString()
|
||||
});
|
||||
db.SaveChanges();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 日志写入失败绝不能影响业务(如数据库不可用时)
|
||||
}
|
||||
}
|
||||
|
||||
private static LogLevelEntity Map(LogLevel level) => level switch
|
||||
{
|
||||
LogLevel.Trace => LogLevelEntity.Trace,
|
||||
LogLevel.Debug => LogLevelEntity.Debug,
|
||||
LogLevel.Information => LogLevelEntity.Info,
|
||||
LogLevel.Warning => LogLevelEntity.Warning,
|
||||
LogLevel.Error => LogLevelEntity.Error,
|
||||
LogLevel.Critical => LogLevelEntity.Critical,
|
||||
_ => LogLevelEntity.Info
|
||||
};
|
||||
|
||||
private static string ResolveSource(string category)
|
||||
{
|
||||
if (category.StartsWith("Plugin.")) return "Plugin";
|
||||
if (category.Contains(".PluginHost.")) return "PluginHost";
|
||||
if (category.Contains(".Host.")) return "Host";
|
||||
if (category == "WebSocket") return "Host";
|
||||
return "System";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user