You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.8 KiB
137 lines
3.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sog.Log
|
|
{
|
|
public class UserLogger
|
|
{
|
|
private int m_logLevel;
|
|
|
|
/// <summary>
|
|
/// 是否log所有玩家,外网请关闭
|
|
/// </summary>
|
|
private bool m_logAllUser;
|
|
|
|
private long[] m_logSomeUser;
|
|
|
|
private string m_logPath;
|
|
private string m_logFileName;
|
|
|
|
//这个加了后就不会删,只是占用点内存,无所谓了
|
|
private Dictionary<long, Logger> m_allUser = new Dictionary<long, Logger>();
|
|
|
|
public void Init(string path, string filename, int logLevel, bool logAllUser,long[] logSomeUser)
|
|
{
|
|
m_logAllUser = logAllUser;
|
|
m_logLevel = logLevel;
|
|
m_logSomeUser = logSomeUser;
|
|
|
|
m_logPath = path;
|
|
m_logFileName = filename;
|
|
|
|
m_allUser.Clear();
|
|
}
|
|
|
|
public int GetLogLevel()
|
|
{
|
|
return m_logLevel;
|
|
}
|
|
|
|
public bool NeedLogUser(long uid)
|
|
{
|
|
if(m_logAllUser)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if(m_logSomeUser == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
for(int i=0; i< m_logSomeUser.Length; i++)
|
|
{
|
|
if(m_logSomeUser[i] == uid)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
public void LogByLevel(long uid, int logLevel, string strFormat, params object[] argvList)
|
|
{
|
|
//先判断等级
|
|
if (logLevel < m_logLevel)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//这个uid是否需要log
|
|
if (!NeedLogUser(uid))
|
|
{
|
|
return;
|
|
}
|
|
|
|
|
|
Logger logger;
|
|
if (false == m_allUser.TryGetValue(uid, out logger))
|
|
{
|
|
logger = new Logger(m_logPath, m_logFileName + "_" + uid.ToString() + ".log", m_logLevel);
|
|
m_allUser.Add(uid, logger);
|
|
}
|
|
|
|
if (OSUtils.IsWindows() == false)
|
|
{
|
|
//外网版本,log错误不影响逻辑
|
|
try
|
|
{
|
|
string strLog = string.Format(strFormat, argvList);
|
|
logger.WriteLog(logLevel, strLog);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.WriteLog(logLevel, ex.Message);
|
|
logger.WriteLog(logLevel, ex.Source);
|
|
logger.WriteLog(logLevel, ex.StackTrace);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//windows版本,为了发现问题,log错误直接异常,不继续处理了
|
|
string strLog = string.Format(strFormat, argvList);
|
|
logger.WriteLog(logLevel, strLog);
|
|
}
|
|
|
|
//string strLog = string.Format(strFormat, argvList);
|
|
|
|
//logger.WriteLog(logLevel, strLog);
|
|
}
|
|
|
|
public void LogTraceDetail(long uid, string strFormat, params object[] argvList)
|
|
{
|
|
LogByLevel(uid, LogLevel.TraceDetail, strFormat, argvList);
|
|
}
|
|
|
|
public void LogTrace(long uid,string strFormat, params object[] argvList)
|
|
{
|
|
LogByLevel(uid,LogLevel.Trace, strFormat, argvList);
|
|
}
|
|
public void LogDebug(long uid, string strFormat, params object[] argvList)
|
|
{
|
|
LogByLevel(uid,LogLevel.Debug, strFormat, argvList);
|
|
}
|
|
public void LogError(long uid, string strFormat, params object[] argvList)
|
|
{
|
|
LogByLevel(uid,LogLevel.Error, strFormat, argvList);
|
|
}
|
|
public void LogFatal(long uid, string strFormat, params object[] argvList)
|
|
{
|
|
LogByLevel(uid,LogLevel.Fatal, strFormat, argvList);
|
|
}
|
|
}
|
|
}
|
|
|