📄 logger.cs
字号:
namespace NCindy.Util.Logging
{
using NCindy.Util;
using System;
using System.Text;
internal class Logger : ILogger
{
private readonly LoggingLevel level;
private const string logFormat = "[{0}]-[{1}]: In {2} class, Message: {3}";
private readonly Type type;
public Logger(Type type)
{
this.type = type;
this.level = (LoggingLevel) Enum.Parse(typeof(LoggingLevel), Configuration.LoggingLevel, true);
}
private string BuildLogMessage(string message, Exception exception, LoggingLevel level)
{
StringBuilder builder = new StringBuilder(0x400);
builder.AppendFormat("[{0}]-[{1}]: In {2} class, Message: {3}", new object[] { DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), level, this.type.FullName, message });
if (exception != null)
{
builder.AppendFormat("\r\nException:{0}", exception.ToString());
}
return builder.ToString();
}
public void Debug(string message)
{
this.Debug(message, null);
}
public void Debug(string message, Exception exception)
{
try
{
if (this.IsDebugEnabled)
{
this.WriteLog(message, exception, LoggingLevel.Debug);
}
}
catch
{
}
}
public void Error(string message)
{
this.Error(message, null);
}
public void Error(string message, Exception exception)
{
try
{
if (this.IsErrorEnabled)
{
this.WriteLog(message, exception, LoggingLevel.Error);
}
}
catch
{
}
}
public void Fatal(string message)
{
this.Fatal(message, null);
}
public void Fatal(string message, Exception exception)
{
try
{
if (this.IsFatalEnabled)
{
this.WriteLog(message, exception, LoggingLevel.Fatal);
}
}
catch
{
}
}
public void Info(string message)
{
this.Info(message, null);
}
public void Info(string message, Exception exception)
{
try
{
if (this.IsDebugEnabled)
{
this.WriteLog(message, exception, LoggingLevel.Info);
}
}
catch
{
}
}
public void Warn(string message)
{
this.Warn(message, null);
}
public void Warn(string message, Exception exception)
{
try
{
if (this.IsWarnEnabled)
{
this.WriteLog(message, exception, LoggingLevel.Warn);
}
}
catch
{
}
}
private void WriteLog(string message, Exception exception, LoggingLevel level)
{
LogWriter.WriteLog(this.BuildLogMessage(message, exception, level));
}
public bool IsDebugEnabled
{
get
{
return (this.level <= LoggingLevel.Debug);
}
}
public bool IsErrorEnabled
{
get
{
return (this.level <= LoggingLevel.Error);
}
}
public bool IsFatalEnabled
{
get
{
return (this.level <= LoggingLevel.Fatal);
}
}
public bool IsInfoEnabled
{
get
{
return (this.level <= LoggingLevel.Info);
}
}
public bool IsWarnEnabled
{
get
{
return (this.level <= LoggingLevel.Warn);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -