Files
DanMuRead/BarrageGrab/Logger.cs
T

193 lines
6.6 KiB
C#
Raw Normal View History

2023-08-17 20:31:20 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;
2023-11-23 16:43:21 +08:00
using BarrageGrab.Modles.JsonEntity;
using System.IO;
2023-08-17 20:31:20 +08:00
namespace BarrageGrab
{
public static class Logger
2023-11-23 16:43:21 +08:00
{
2023-08-17 20:31:20 +08:00
private static NLog.Config.ISetupBuilder builder = LogManager.Setup().LoadConfigurationFromFile("nlog.config");
private static readonly NLog.Logger logger = builder.GetLogger("*");
public static void PrintColor(string message, ConsoleColor foreground = ConsoleColor.White)
{
var color = Console.ForegroundColor;
Console.ForegroundColor = foreground;
Console.WriteLine(message);
Console.ForegroundColor = color;
}
public static void LogSucc(string message)
{
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
logger.Info(message);
Console.ForegroundColor = color;
}
2023-08-17 20:31:20 +08:00
// 记录日志方法
public static void LogTrace(string message)
{
logger.Trace(message);
}
public static void LogDebug(string message)
{
logger.Debug(message);
}
public static void LogInfo(string message)
{
logger.Info(message);
}
public static void LogWarn(string message)
{
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
2023-08-17 20:31:20 +08:00
logger.Warn(message);
Console.ForegroundColor = color;
2023-08-17 20:31:20 +08:00
}
public static void LogError(string message)
{
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
2023-08-17 20:31:20 +08:00
logger.Error(message);
Console.ForegroundColor = color;
2023-08-17 20:31:20 +08:00
}
public static void LogError(Exception ex, string message)
{
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
2023-08-17 20:31:20 +08:00
logger.Error(ex, message);
Console.ForegroundColor = color;
2023-08-17 20:31:20 +08:00
}
public static void LogFatal(string message)
{
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
2023-08-17 20:31:20 +08:00
logger.Fatal(message);
Console.ForegroundColor = color;
2023-08-17 20:31:20 +08:00
}
public static void LogFatal(Exception ex, string message)
{
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
2023-08-17 20:31:20 +08:00
logger.Fatal(ex, message);
Console.ForegroundColor = color;
2023-08-17 20:31:20 +08:00
}
2023-11-23 16:43:21 +08:00
2024-01-09 22:39:08 +08:00
/// <summary>
/// 写入弹幕日志
/// </summary>
/// <param name="type">弹幕类型</param>
/// <param name="msg">弹幕内容</param>
2023-11-23 16:43:21 +08:00
public static void LogBarrage(PackMsgType type, Msg msg)
{
2024-03-29 17:34:14 +08:00
if (!AppSetting.Current.BarrageLog) return;
2023-11-23 16:43:21 +08:00
if (type == PackMsgType.无) return;
2024-03-29 17:34:14 +08:00
if (AppSetting.Current.LogFilter.Any() && !AppSetting.Current.LogFilter.Contains(type.GetHashCode())) return;
2023-11-23 16:43:21 +08:00
try
{
var dir = Path.Combine(AppContext.BaseDirectory, "logs", "弹幕日志");
var room = AppRuntime.RoomCaches.GetCachedWebRoomInfo(msg.RoomId.ToString());
var date = DateTime.Now.ToString("yyyy年MM月dd日直播");
dir = Path.Combine(dir, $"({room.WebRoomId}){room?.Owner?.Nickname ?? "-1"}", date);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var path = Path.Combine(dir, type + ".txt");
if (!File.Exists(path))
{
File.Create(path).Close();
}
var writer = new StreamWriter(path, true, Encoding.UTF8);
var line = LogText(msg, type);
writer.WriteLine(line);
writer.Close();
}
catch (Exception ex)
{
LogWarn("弹幕日志写入失败," + ex.Message);
return;
2024-01-09 22:39:08 +08:00
}
2023-11-23 16:43:21 +08:00
}
private static string LogText(Msg msg, PackMsgType barType)
{
var rinfo = AppRuntime.RoomCaches.GetCachedWebRoomInfo(msg.RoomId.ToString());
var text = $"{DateTime.Now.ToString("HH:mm:ss")} [{barType}]";
if (msg.User != null)
{
text += $" [{msg.User?.GenderToString()}] ";
}
2024-03-29 17:34:14 +08:00
ConsoleColor color = AppSetting.Current.ColorMap[barType].Item1;
2023-11-23 16:43:21 +08:00
var append = msg.Content;
switch (barType)
{
case PackMsgType.弹幕消息: append = $"{msg?.User?.Nickname}: {msg.Content}"; break;
case PackMsgType.下播: append = $"直播已结束"; break;
default: break;
}
text += append;
return text;
}
2024-01-09 22:39:08 +08:00
/// <summary>
/// 写入弹幕抓包日志
/// </summary>
/// <param name="group">分组名</param>
/// <param name="name">包名</param>
/// <param name="buff">数据内容</param>
/// <param name="maxCount">分组下最大存储包数量</param>
public static void LogBarragePack(string group, string name, byte[] buff, int maxCount = 5)
{
if (buff.Length <= 10) return;
if (group.IsNullOrWhiteSpace()) return;
if (name.IsNullOrWhiteSpace()) return;
var filename = name + ".bin";
var dir = Path.Combine(AppContext.BaseDirectory, "logs", "弹幕包解析", group);
var fullPath = Path.Combine(dir, filename);
try
{
//获取该文件在该目录下的数量
var fiels = Directory.GetFiles(dir, filename);
var count = fiels.Length;
if (count > 0)
{
filename = $"{name}({count}).bin";
fullPath = Path.Combine(dir, filename);
}
File.WriteAllBytes(fullPath, buff);
if (++count > maxCount)
{
//删除最早的文件
var first = fiels.Select(s => new FileInfo(s)).OrderBy(o => o.CreationTime).FirstOrDefault();
2024-01-09 22:39:08 +08:00
File.Delete(first.FullName);
}
}
catch (Exception ex)
{
Logger.LogWarn("写入包日志时失败,错误信息:" + ex.Message);
}
}
2023-08-17 20:31:20 +08:00
}
}