提交 f6d95742 编写于 作者: T tanghai

增加延迟构造log,建议使用,防止开启不同级别的log仍然构建字符串产生gc

上级 1014fe1f
namespace ETModel
{
public abstract class ALogDecorater
{
protected const string SEP = " ";
private int level;
protected readonly ALogDecorater decorater;
protected ALogDecorater(ALogDecorater decorater = null)
{
this.decorater = decorater;
this.Level = 0;
}
protected int Level
{
get
{
return this.level;
}
set
{
this.level = value;
if (this.decorater != null)
{
this.decorater.Level = value + 1;
}
}
}
public virtual string Decorate(string message)
{
if (this.decorater == null)
{
return message;
}
return this.decorater.Decorate(message);
}
}
}
\ No newline at end of file
......@@ -8,5 +8,11 @@
void Debug(string message);
void Error(string message);
void Fatal(string message);
void Trace(string message, params object[] args);
void Warning(string message, params object[] args);
void Info(string message, params object[] args);
void Debug(string message, params object[] args);
void Error(string message, params object[] args);
void Fatal(string message, params object[] args);
}
}
\ No newline at end of file
......@@ -2,42 +2,68 @@
namespace ETModel
{
public class NLogAdapter: ALogDecorater, ILog
public class NLogAdapter: ILog
{
private readonly Logger logger = LogManager.GetLogger("Logger");
public NLogAdapter(ALogDecorater decorater = null): base(decorater)
{
}
public void Trace(string message)
{
this.logger.Trace(this.Decorate(message));
this.logger.Trace(message);
}
public void Warning(string message)
{
this.logger.Warn(this.Decorate(message));
this.logger.Warn(message);
}
public void Info(string message)
{
this.logger.Info(this.Decorate(message));
this.logger.Info(message);
}
public void Debug(string message)
{
this.logger.Debug(this.Decorate(message));
this.logger.Debug(message);
}
public void Error(string message)
{
this.logger.Error(this.Decorate(message));
this.logger.Error(message);
}
public void Fatal(string message)
{
this.logger.Fatal(this.Decorate(message));
this.logger.Fatal(message);
}
}
public void Trace(string message, params object[] args)
{
this.logger.Trace(message, args);
}
public void Warning(string message, params object[] args)
{
this.logger.Warn(message, args);
}
public void Info(string message, params object[] args)
{
this.logger.Info(message, args);
}
public void Debug(string message, params object[] args)
{
this.logger.Debug(message, args);
}
public void Error(string message, params object[] args)
{
this.logger.Error(message, args);
}
public void Fatal(string message, params object[] args)
{
this.logger.Fatal(message, args);
}
}
}
\ No newline at end of file
using System.Diagnostics;
using System.IO;
namespace ETModel
{
internal class StackInfoDecorater: ALogDecorater
{
public StackInfoDecorater(ALogDecorater decorater = null): base(decorater)
{
this.FileName = true;
this.FileLineNumber = true;
}
public bool FileName { get; set; }
public bool FileLineNumber { get; set; }
public override string Decorate(string message)
{
if (this.decorater != null)
{
message = this.decorater.Decorate(message);
}
if (!this.FileLineNumber && !this.FileName)
{
return message;
}
string extraInfo = "";
StackTrace stackTrace = new StackTrace(true);
StackFrame frame = stackTrace.GetFrame(this.Level + 3);
if (this.FileName)
{
string fileName = Path.GetFileName(frame.GetFileName());
extraInfo += fileName + " ";
}
if (this.FileLineNumber)
{
int fileLineNumber = frame.GetFileLineNumber();
extraInfo += fileLineNumber + " ";
}
return extraInfo + message;
}
}
}
\ No newline at end of file
......@@ -4,6 +4,11 @@ namespace ETHotfix
{
public static class Log
{
public static void Trace(string msg)
{
ETModel.Log.Trace(msg);
}
public static void Warning(string msg)
{
ETModel.Log.Warning(msg);
......@@ -29,9 +34,38 @@ namespace ETHotfix
ETModel.Log.Debug(msg);
}
public static void Trace(string message, params object[] args)
{
ETModel.Log.Trace(message, args);
}
public static void Warning(string message, params object[] args)
{
ETModel.Log.Warning(message, args);
}
public static void Info(string message, params object[] args)
{
ETModel.Log.Info(message, args);
}
public static void Debug(string message, params object[] args)
{
ETModel.Log.Debug(message, args);
}
public static void Error(string message, params object[] args)
{
ETModel.Log.Error(message, args);
}
public static void Fatal(string message, params object[] args)
{
ETModel.Log.Fatal(message, args);
}
public static void Msg(object msg)
{
Debug(Dumper.DumpAsString(msg));
}
}
......
......@@ -8,30 +8,65 @@ namespace ETModel
{
UnityEngine.Debug.Log(msg);
}
public static void Warning(string msg)
public static void Debug(string msg)
{
UnityEngine.Debug.LogWarning(msg);
UnityEngine.Debug.Log(msg);
}
public static void Info(string msg)
{
UnityEngine.Debug.Log(msg);
}
public static void Error(Exception e)
public static void Warning(string msg)
{
UnityEngine.Debug.LogError(e.ToString());
UnityEngine.Debug.LogWarning(msg);
}
public static void Error(string msg)
{
UnityEngine.Debug.LogError(msg);
}
public static void Error(Exception e)
{
UnityEngine.Debug.LogException(e);
}
public static void Debug(string msg)
public static void Fatal(string msg)
{
UnityEngine.Debug.Log(msg);
UnityEngine.Debug.LogAssertion(msg);
}
public static void Trace(string message, params object[] args)
{
UnityEngine.Debug.LogFormat(message, args);
}
public static void Warning(string message, params object[] args)
{
UnityEngine.Debug.LogWarningFormat(message, args);
}
public static void Info(string message, params object[] args)
{
UnityEngine.Debug.LogFormat(message, args);
}
public static void Debug(string message, params object[] args)
{
UnityEngine.Debug.LogFormat(message, args);
}
public static void Error(string message, params object[] args)
{
UnityEngine.Debug.LogErrorFormat(message, args);
}
public static void Fatal(string message, params object[] args)
{
UnityEngine.Debug.LogAssertionFormat(message, args);
}
public static void Msg(object msg)
......
......@@ -4,10 +4,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Model", "Unity.Model.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.ThirdParty", "Unity.ThirdParty.csproj", "{E15BADD2-3A26-309A-AB0F-DC5B08044350}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Editor", "Unity.Editor.csproj", "{CD311104-1830-B119-81B6-5DBEE2467FFB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Hotfix", "Unity.Hotfix.csproj", "{1066F652-6A89-D1C4-9881-1A19DF7AB80E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Editor", "Unity.Editor.csproj", "{CD311104-1830-B119-81B6-5DBEE2467FFB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -22,14 +22,14 @@ Global
{E15BADD2-3A26-309A-AB0F-DC5B08044350}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E15BADD2-3A26-309A-AB0F-DC5B08044350}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E15BADD2-3A26-309A-AB0F-DC5B08044350}.Release|Any CPU.Build.0 = Release|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Release|Any CPU.Build.0 = Release|Any CPU
{1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Release|Any CPU.Build.0 = Release|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册