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.
194 lines
5.8 KiB
194 lines
5.8 KiB
/*
|
|
Sog 游戏基础库
|
|
2016 by zouwei
|
|
*/
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace Sog.Log
|
|
{
|
|
/// <summary>
|
|
/// 日志文件读写操作
|
|
/// </summary>
|
|
public class Logger
|
|
{
|
|
private string m_filePathName;
|
|
private string m_LogPath;
|
|
private string m_LogFileName;
|
|
private int m_logLevel;
|
|
|
|
//是否滚动文件,缺省滚动,运营日志可关闭
|
|
private bool m_shiftFile = true;
|
|
|
|
public Logger(string logPath, string logFileName, int logLevel)
|
|
{
|
|
SetPathName(logPath, logFileName);
|
|
|
|
m_logLevel = logLevel;
|
|
}
|
|
|
|
public void SetPathName(string logPath, string logFileName)
|
|
{
|
|
m_LogPath = logPath;
|
|
m_LogFileName = logFileName;
|
|
m_filePathName = m_LogPath + "/" + logFileName;
|
|
|
|
try
|
|
{
|
|
//不存在则创建目录
|
|
if (!Directory.Exists(logPath))
|
|
{
|
|
Directory.CreateDirectory(logPath);
|
|
}
|
|
}
|
|
catch(Exception)
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void SetFileName(string logFileName)
|
|
{
|
|
m_LogFileName = logFileName;
|
|
m_filePathName = m_LogPath + "/" + logFileName;
|
|
}
|
|
|
|
public int GetLevel()
|
|
{
|
|
return m_logLevel;
|
|
}
|
|
|
|
public void SetLevel(int logLevel)
|
|
{
|
|
m_logLevel = logLevel;
|
|
}
|
|
|
|
public void CloseShift()
|
|
{
|
|
m_shiftFile = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 不是绝对路径
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string GetFileName()
|
|
{
|
|
return m_LogFileName;
|
|
}
|
|
|
|
internal void WriteLog(int logLevel, string strLog)
|
|
{
|
|
string logmessage = "[" + AppTime.ServerAppTime.GetDateTime().ToString("yyyy-MM-dd HH:mm:ss.fff") + "]" + LogLevel.LevelToString(logLevel) + "|" + strLog;
|
|
//补上一个回车
|
|
if(logmessage[logmessage.Length-1] != '\n')
|
|
{
|
|
logmessage += '\n';
|
|
}
|
|
LogWriteThread.Instance.WriteLog(m_filePathName, logmessage, m_shiftFile);
|
|
}
|
|
|
|
internal void WriteLogNoTime(int logLevel, int frame, string strLog)
|
|
{
|
|
string logmessage = string.Format("[{0}]{1}|{2}", frame, LogLevel.LevelToString(logLevel), strLog);
|
|
//补上一个回车
|
|
if (logmessage[logmessage.Length - 1] != '\n')
|
|
{
|
|
logmessage += '\n';
|
|
}
|
|
LogWriteThread.Instance.WriteLog(m_filePathName, logmessage, m_shiftFile);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// bill使用,写日志的时间由外部传入
|
|
/// 支持线程安全
|
|
/// </summary>
|
|
/// <param name="time"></param>
|
|
/// <param name="strLog"></param>
|
|
public void WriteLogWithTime(DateTime time, string strLog)
|
|
{
|
|
string logmessage = time.ToString("yyyy-MM-dd HH:mm:ss") + "|" + strLog + "\n";
|
|
|
|
LogWriteThread.Instance.WriteLog(m_filePathName, logmessage, m_shiftFile);
|
|
}
|
|
|
|
/// <summary>
|
|
/// TALog使用,写文件不需要打印时间
|
|
/// </summary>
|
|
/// <param name="strLog"></param>
|
|
public void WriteTALog(string strLog)
|
|
{
|
|
string logmessage = strLog + "\r\n";
|
|
LogWriteThread.Instance.WriteLog(m_filePathName, logmessage, m_shiftFile);
|
|
}
|
|
|
|
public void LogByLevel(int logLevel, string strFormat, params object[] argvList)
|
|
{
|
|
if (logLevel >= m_logLevel)
|
|
{
|
|
if(OSUtils.IsWindows() == false)
|
|
{
|
|
//外网版本,log错误不影响逻辑
|
|
try {
|
|
string strLog = string.Format(strFormat, argvList);
|
|
WriteLog(logLevel, strLog);
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
WriteLog(logLevel, ex.Message);
|
|
WriteLog(logLevel, ex.Source);
|
|
WriteLog(logLevel, ex.StackTrace);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//windows版本,为了发现问题,log错误直接异常,不继续处理了
|
|
string strLog = string.Format(strFormat, argvList);
|
|
WriteLog(logLevel, strLog);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void LogByLevelNoFormat(int logLevel, string strMessage)
|
|
{
|
|
if (logLevel >= m_logLevel)
|
|
{
|
|
WriteLog(logLevel, strMessage);
|
|
}
|
|
}
|
|
public void LogTraceDetail(string strFormat, params object[] argvList)
|
|
{
|
|
LogByLevel(LogLevel.TraceDetail, strFormat, argvList);
|
|
}
|
|
public void LogTrace(string strFormat, params object[] argvList)
|
|
{
|
|
LogByLevel(LogLevel.Trace, strFormat, argvList);
|
|
}
|
|
public void LogDebug(string strFormat, params object[] argvList)
|
|
{
|
|
LogByLevel(LogLevel.Debug, strFormat, argvList);
|
|
}
|
|
public void LogError(string strFormat, params object[] argvList)
|
|
{
|
|
LogByLevel(LogLevel.Error, strFormat, argvList);
|
|
}
|
|
public void LogFatal(string strFormat, params object[] argvList)
|
|
{
|
|
LogByLevel(LogLevel.Fatal, strFormat, argvList);
|
|
}
|
|
|
|
|
|
public void LogDebugNoFormat(string strMessage)
|
|
{
|
|
LogByLevelNoFormat(LogLevel.Debug, strMessage);
|
|
}
|
|
|
|
|
|
}
|
|
}
|