295 lines
8.8 KiB
C#
295 lines
8.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 用户操作日志服务
|
|
/// 负责记录、存储和管理用户的各种操作日志
|
|
/// </summary>
|
|
public class UserLogService : IService
|
|
{
|
|
private readonly string _logFilePath;
|
|
private UserLogDatabase _logDatabase;
|
|
private const string LOG_FILE_NAME = "user_logs.json";
|
|
private const int MAX_LOG_ENTRIES = 10000; // 最大日志条数
|
|
|
|
public UserLogService()
|
|
{
|
|
// 使用 persistentDataPath 确保跨平台兼容性
|
|
_logFilePath = Path.Combine(Application.persistentDataPath, LOG_FILE_NAME);
|
|
LoadLogDatabase();
|
|
Debug.Log($"用户日志存储路径: {_logFilePath}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录用户操作日志
|
|
/// </summary>
|
|
/// <param name="username">用户名</param>
|
|
/// <param name="operation">操作类型</param>
|
|
/// <param name="details">操作详情</param>
|
|
/// <param name="successful">操作是否成功</param>
|
|
public void LogUserOperation(string username, string operation, string details = null, bool successful = true)
|
|
{
|
|
var logEvent = new UserLogEvent(username, operation, details, successful)
|
|
{
|
|
Id = GetNextId()
|
|
};
|
|
|
|
_logDatabase.UserLogs.Add(logEvent);
|
|
|
|
// 如果日志条数超过限制,删除最旧的日志
|
|
if (_logDatabase.UserLogs.Count > MAX_LOG_ENTRIES)
|
|
{
|
|
int removeCount = _logDatabase.UserLogs.Count - MAX_LOG_ENTRIES;
|
|
_logDatabase.UserLogs.RemoveRange(0, removeCount);
|
|
}
|
|
|
|
SaveLogDatabase();
|
|
Debug.Log($"记录用户操作: {username} - {operation} - {(successful ? "成功" : "失败")}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录系统日志(保持向后兼容)
|
|
/// </summary>
|
|
/// <param name="account">账户</param>
|
|
/// <param name="operationContent">操作内容</param>
|
|
public void LogSystemOperation(string account, string operationContent)
|
|
{
|
|
var logEvent = new UseLogEvent(account, operationContent)
|
|
{
|
|
Id = GetNextId()
|
|
};
|
|
|
|
_logDatabase.SystemLogs.Add(logEvent);
|
|
|
|
// 如果日志条数超过限制,删除最旧的日志
|
|
if (_logDatabase.SystemLogs.Count > MAX_LOG_ENTRIES)
|
|
{
|
|
int removeCount = _logDatabase.SystemLogs.Count - MAX_LOG_ENTRIES;
|
|
_logDatabase.SystemLogs.RemoveRange(0, removeCount);
|
|
}
|
|
|
|
SaveLogDatabase();
|
|
Debug.Log($"记录系统操作: {account} - {operationContent}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有用户日志
|
|
/// </summary>
|
|
/// <returns>用户日志列表</returns>
|
|
public List<UserLogEvent> GetAllUserLogs()
|
|
{
|
|
return new List<UserLogEvent>(_logDatabase.UserLogs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有系统日志(保持向后兼容)
|
|
/// </summary>
|
|
/// <returns>系统日志列表</returns>
|
|
public List<UseLogEvent> GetAllSystemLogs()
|
|
{
|
|
return new List<UseLogEvent>(_logDatabase.SystemLogs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定用户的日志
|
|
/// </summary>
|
|
/// <param name="username">用户名</param>
|
|
/// <returns>用户的日志列表</returns>
|
|
public List<UserLogEvent> GetUserLogs(string username)
|
|
{
|
|
return _logDatabase.UserLogs.FindAll(log =>
|
|
log.Username.Equals(username, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定时间范围内的日志
|
|
/// </summary>
|
|
/// <param name="startTime">开始时间</param>
|
|
/// <param name="endTime">结束时间</param>
|
|
/// <returns>时间范围内的日志列表</returns>
|
|
public List<UserLogEvent> GetLogsByTimeRange(DateTime startTime, DateTime endTime)
|
|
{
|
|
return _logDatabase.UserLogs.FindAll(log =>
|
|
log.Timestamp >= startTime && log.Timestamp <= endTime);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定操作类型的日志
|
|
/// </summary>
|
|
/// <param name="operation">操作类型</param>
|
|
/// <returns>操作类型的日志列表</returns>
|
|
public List<UserLogEvent> GetLogsByOperation(string operation)
|
|
{
|
|
return _logDatabase.UserLogs.FindAll(log =>
|
|
log.Operation.Equals(operation, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清空所有日志
|
|
/// </summary>
|
|
public void ClearAllLogs()
|
|
{
|
|
_logDatabase.UserLogs.Clear();
|
|
_logDatabase.SystemLogs.Clear();
|
|
_logDatabase.NextId = 1;
|
|
SaveLogDatabase();
|
|
Debug.Log("已清空所有日志记录");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清空用户日志
|
|
/// </summary>
|
|
public void ClearUserLogs()
|
|
{
|
|
_logDatabase.UserLogs.Clear();
|
|
SaveLogDatabase();
|
|
Debug.Log("已清空用户日志记录");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清空系统日志
|
|
/// </summary>
|
|
public void ClearSystemLogs()
|
|
{
|
|
_logDatabase.SystemLogs.Clear();
|
|
SaveLogDatabase();
|
|
Debug.Log("已清空系统日志记录");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取日志统计信息
|
|
/// </summary>
|
|
/// <returns>日志统计信息</returns>
|
|
public LogStatistics GetLogStatistics()
|
|
{
|
|
var stats = new LogStatistics
|
|
{
|
|
TotalUserLogs = _logDatabase.UserLogs.Count,
|
|
TotalSystemLogs = _logDatabase.SystemLogs.Count,
|
|
SuccessfulOperations = _logDatabase.UserLogs.FindAll(log => log.IsSuccessful).Count,
|
|
FailedOperations = _logDatabase.UserLogs.FindAll(log => !log.IsSuccessful).Count
|
|
};
|
|
|
|
if (_logDatabase.UserLogs.Count > 0)
|
|
{
|
|
stats.FirstLogTime = _logDatabase.UserLogs[0].Timestamp;
|
|
stats.LastLogTime = _logDatabase.UserLogs[_logDatabase.UserLogs.Count - 1].Timestamp;
|
|
}
|
|
|
|
return stats;
|
|
}
|
|
|
|
private int GetNextId()
|
|
{
|
|
return _logDatabase.NextId++;
|
|
}
|
|
|
|
private void LoadLogDatabase()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(_logFilePath))
|
|
{
|
|
string json = File.ReadAllText(_logFilePath);
|
|
_logDatabase = JsonUtility.FromJson<UserLogDatabase>(json);
|
|
|
|
if (_logDatabase == null)
|
|
{
|
|
Debug.LogWarning("日志数据库格式错误,创建新的数据库");
|
|
CreateDefaultLogDatabase();
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"成功加载日志数据库,共 {_logDatabase.UserLogs.Count} 条用户日志,{_logDatabase.SystemLogs.Count} 条系统日志");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CreateDefaultLogDatabase();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogError($"加载日志数据库失败: {ex.Message}");
|
|
CreateDefaultLogDatabase();
|
|
}
|
|
|
|
if (_logDatabase == null)
|
|
{
|
|
CreateDefaultLogDatabase();
|
|
}
|
|
}
|
|
|
|
private void SaveLogDatabase()
|
|
{
|
|
try
|
|
{
|
|
string directory = Path.GetDirectoryName(_logFilePath);
|
|
if (!Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
|
|
string json = JsonUtility.ToJson(_logDatabase, true);
|
|
File.WriteAllText(_logFilePath, json);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogError($"保存日志数据库失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private void CreateDefaultLogDatabase()
|
|
{
|
|
_logDatabase = new UserLogDatabase
|
|
{
|
|
Version = "1.0",
|
|
NextId = 1,
|
|
UserLogs = new List<UserLogEvent>(),
|
|
SystemLogs = new List<UseLogEvent>()
|
|
};
|
|
|
|
// 添加初始化日志
|
|
LogUserOperation("system", "系统初始化", "创建日志数据库", true);
|
|
|
|
SaveLogDatabase();
|
|
Debug.Log("创建默认日志数据库");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取日志文件路径(用于调试)
|
|
/// </summary>
|
|
/// <returns>日志文件路径</returns>
|
|
public string GetLogFilePath()
|
|
{
|
|
return _logFilePath;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用户日志数据库结构
|
|
/// </summary>
|
|
[System.Serializable]
|
|
public class UserLogDatabase
|
|
{
|
|
public string Version = "1.0";
|
|
public int NextId = 1;
|
|
public List<UserLogEvent> UserLogs = new List<UserLogEvent>();
|
|
public List<UseLogEvent> SystemLogs = new List<UseLogEvent>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 日志统计信息
|
|
/// </summary>
|
|
[System.Serializable]
|
|
public class LogStatistics
|
|
{
|
|
public int TotalUserLogs;
|
|
public int TotalSystemLogs;
|
|
public int SuccessfulOperations;
|
|
public int FailedOperations;
|
|
public DateTime FirstLogTime;
|
|
public DateTime LastLogTime;
|
|
} |