123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- using System;
- using System.Diagnostics;
- namespace KYFramework
- {
- public class Logger
- {
- private static Logger instance;
- public static Logger Instance
- {
- get
- {
- if (instance == null) instance = new Logger();
- return instance;
- }
- }
- private ILog iLog;
- public ILog ILog
- {
- get
- {
- return iLog;
- }
- set
- {
- this.iLog = value;
- }
- }
-
- private const int TraceLevel = 1;
- private const int DebugLevel = 2;
- private const int InfoLevel = 3;
- private const int WarningLevel = 4;
- private bool CheckLogLevel(int level)
- {
- if (OptionComponent.Instance == null)
- {
- return true;
- }
- return OptionComponent.Instance.option.LogLevel <= level;
- }
-
- public void Trace(string msg)
- {
- if (!CheckLogLevel(DebugLevel))
- {
- return;
- }
- StackTrace st = new StackTrace(2, true);
- Logger.Instance.Trace($"{msg}\n{st}");
- }
- public void Debug(string msg)
- {
- if (!CheckLogLevel(DebugLevel))
- {
- return;
- }
- this.iLog?.Debug(msg);
- }
- public void Info(string msg)
- {
- if (!CheckLogLevel(InfoLevel))
- {
- return;
- }
- this.iLog?.Info(msg);
- }
- public void TraceInfo(string msg)
- {
- if (!CheckLogLevel(InfoLevel))
- {
- return;
- }
- StackTrace st = new StackTrace(2, true);
- this.iLog?.Trace($"{msg}\n{st}");
- }
- public void Warning(string msg)
- {
- if (!CheckLogLevel(WarningLevel))
- {
- return;
- }
- this.iLog?.Warning(msg);
- }
- public void Error(string msg)
- {
- StackTrace st = new StackTrace(2, true);
- this.iLog?.Error($"{msg}\n{st}");
- }
- public void Error(Exception e)
- {
- if (e.Data.Contains("StackTrace"))
- {
- this.iLog?.Error($"{e.Data["StackTrace"]}\n{e}");
- return;
- }
- string str = e.ToString();
- this.iLog?.Error(str);
- }
- public void Trace(string message, params object[] args)
- {
- if (!CheckLogLevel(TraceLevel))
- {
- return;
- }
- StackTrace st = new StackTrace(2, true);
- this.iLog?.Trace($"{string.Format(message, args)}\n{st}");
- }
- public void Warning(string message, params object[] args)
- {
- if (!CheckLogLevel(WarningLevel))
- {
- return;
- }
- this.iLog?.Warning(string.Format(message, args));
- }
- public void Info(string message, params object[] args)
- {
- if (!CheckLogLevel(InfoLevel))
- {
- return;
- }
- this.iLog?.Info(string.Format(message, args));
- }
- public void Debug(string message, params object[] args)
- {
- if (!CheckLogLevel(DebugLevel))
- {
- return;
- }
- this.iLog?.Debug(string.Format(message, args));
- }
- public void Error(string message, params object[] args)
- {
- StackTrace st = new StackTrace(2, true);
- string s = string.Format(message, args) + '\n' + st;
- this.iLog?.Error(s);
- }
-
-
- }
- }
|