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.
235 lines
7.2 KiB
235 lines
7.2 KiB
using Apewer.Internals;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
namespace Apewer
|
|
{
|
|
|
|
/// <summary>日志记录程序。</summary>
|
|
public class Logger
|
|
{
|
|
|
|
private string _key = Guid.NewGuid().ToString("n");
|
|
private string _target = "";
|
|
private bool _enabled = true;
|
|
|
|
/// <summary>异常。</summary>
|
|
public event Event<Exception> ExceptionEvent;
|
|
|
|
/// <summary>调试。</summary>
|
|
public event Event<string> DebugEvent;
|
|
|
|
/// <summary>文本。</summary>
|
|
public event Event<string> TextEvent;
|
|
|
|
/// <summary>信息。</summary>
|
|
public event Event<string> InfomationEvent;
|
|
|
|
/// <summary>注意。</summary>
|
|
public event Event<string> WarningEvent;
|
|
|
|
/// <summary>错误。</summary>
|
|
public event Event<string> ErrorEvent;
|
|
|
|
/// <summary>自定义。</summary>
|
|
public event Event<object> CustomEvent;
|
|
|
|
/// <summary>已启用。</summary>
|
|
public bool Enabled { get { return _enabled; } set { _enabled = value; } }
|
|
|
|
/// <summary>唯一标识。</summary>
|
|
public string Key { get { return _key; } }
|
|
|
|
/// <summary>目标。</summary>
|
|
public string Target { get { return _target; } protected set { _target = value ?? ""; } }
|
|
|
|
internal void Invoke(LogItem item)
|
|
{
|
|
if (item == null) return;
|
|
switch (item.Type)
|
|
{
|
|
case LogType.Debug:
|
|
if (DebugEvent != null) DebugEvent(this, item.Content);
|
|
break;
|
|
case LogType.Text:
|
|
if (TextEvent != null) TextEvent(this, item.Content);
|
|
break;
|
|
case LogType.Infomation:
|
|
if (InfomationEvent != null) InfomationEvent(this, item.Content);
|
|
break;
|
|
case LogType.Warning:
|
|
if (WarningEvent != null) WarningEvent(this, item.Content);
|
|
break;
|
|
case LogType.Error:
|
|
if (ErrorEvent != null) ErrorEvent(this, item.Content);
|
|
break;
|
|
case LogType.Exception:
|
|
if (ExceptionEvent != null) ExceptionEvent(this, item.Exception);
|
|
break;
|
|
case LogType.Custom:
|
|
if (CustomEvent != null) CustomEvent(this, item.Custom);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>异常。</summary>
|
|
public void Exception(Exception value)
|
|
{
|
|
if (value == null) return;
|
|
var item = new LogItem(LogType.Exception);
|
|
item.Logger = this;
|
|
item.Target = _target;
|
|
try { item.Content = value.ToString(); } catch { }
|
|
}
|
|
|
|
/// <summary>自定义。</summary>
|
|
public void Custom(object value)
|
|
{
|
|
if (value == null) return;
|
|
var item = new LogItem(LogType.Custom);
|
|
item.Logger = this;
|
|
item.Target = _target;
|
|
item.Custom = value;
|
|
}
|
|
|
|
/// <summary>调试。</summary>
|
|
public void Debug(string value)
|
|
{
|
|
if (value == null) return;
|
|
var item = new LogItem(LogType.Debug);
|
|
item.Logger = this;
|
|
item.Target = _target;
|
|
item.Content = value;
|
|
}
|
|
|
|
/// <summary>文本。</summary>
|
|
public void Text(string value)
|
|
{
|
|
if (value == null) return;
|
|
var item = new LogItem(LogType.Text);
|
|
item.Logger = this;
|
|
item.Target = _target;
|
|
item.Content = value;
|
|
}
|
|
|
|
/// <summary>信息。</summary>
|
|
public void Infomation(string value)
|
|
{
|
|
if (value == null) return;
|
|
var item = new LogItem(LogType.Infomation);
|
|
item.Logger = this;
|
|
item.Target = _target;
|
|
item.Content = value;
|
|
}
|
|
|
|
/// <summary>注意。</summary>
|
|
public void Warning(string value)
|
|
{
|
|
if (value == null) return;
|
|
var item = new LogItem(LogType.Warning);
|
|
item.Logger = this;
|
|
item.Target = _target;
|
|
item.Content = value;
|
|
}
|
|
|
|
/// <summary>错误。</summary>
|
|
public void Error(string value)
|
|
{
|
|
if (value == null) return;
|
|
var item = new LogItem(LogType.Error);
|
|
item.Logger = this;
|
|
item.Target = _target;
|
|
item.Content = value;
|
|
}
|
|
|
|
private Logger(string target)
|
|
{
|
|
_target = target ?? "";
|
|
}
|
|
|
|
private static Event<Exception> ExceptionDefaultCallback = null;
|
|
private static Event<object> CustomDefaultCallback = null;
|
|
private static Event<string> DebugDefaultCallback = null;
|
|
private static Event<string> TextDefaultCallback = null;
|
|
private static Event<string> InfomationDefaultCallback = null;
|
|
private static Event<string> WarningDefaultCallback = null;
|
|
private static Event<string> ErrorDefaultCallback = null;
|
|
|
|
private static void Logger_ExceptionEvent(object sender, Exception value)
|
|
{
|
|
if (ExceptionDefaultCallback != null) ExceptionDefaultCallback(sender, value);
|
|
}
|
|
|
|
private static void Logger_CustomEvent(object sender, object value)
|
|
{
|
|
if (ExceptionDefaultCallback != null) CustomDefaultCallback(sender, value);
|
|
}
|
|
|
|
private static void Logger_DebugEvent(object sender, string value)
|
|
{
|
|
if (ExceptionDefaultCallback != null) DebugDefaultCallback(sender, value);
|
|
}
|
|
|
|
private static void Logger_TextEvent(object sender, string value)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
private static void Logger_InfomationEvent(object sender, string value)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
private static void Logger_WarningEvent(object sender, string value)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
private static void Logger_ErrorEvent(object sender, string value)
|
|
{
|
|
|
|
}
|
|
|
|
private Logger(Type target)
|
|
{
|
|
try
|
|
{
|
|
if (target != null)
|
|
{
|
|
_target = target.FullName;
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
private Logger(object target)
|
|
{
|
|
try
|
|
{
|
|
if (target != null)
|
|
{
|
|
_target = target.GetType().FullName;
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
/// <summary>默认的日志记录程序。</summary>
|
|
public static Logger Default(object target)
|
|
{
|
|
var logger = new Logger(target);
|
|
logger.CustomEvent += Logger_CustomEvent;
|
|
logger.ExceptionEvent += Logger_ExceptionEvent;
|
|
logger.DebugEvent += Logger_DebugEvent;
|
|
logger.TextEvent += Logger_TextEvent;
|
|
logger.InfomationEvent += Logger_InfomationEvent;
|
|
logger.WarningEvent += Logger_WarningEvent;
|
|
logger.ErrorEvent += Logger_ErrorEvent;
|
|
return logger;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|