public sealed class LogHelper
{
/// <summary>
/// 输出 ERROR 日志
/// </summary>
/// <param name="logger"></param>
/// <param name="message">文本消息</param>
public static void Error(Logger logger, string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Log(logger, LogLevel.Error, message, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// 输出 ERROR 日志
/// </summary>
/// <param name="logger"></param>
/// <param name="message">异常消息</param>
public static void Error(Logger logger, Exception throwable,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Log(logger, LogLevel.Error, throwable, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// 输出 WARN 日志
/// </summary>
/// <param name="logger"></param>
/// <param name="message">文本消息</param>
public static void Warn(Logger logger, string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Log(logger, LogLevel.Warn, message, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// 输出 WARN 日志
/// </summary>
/// <param name="logger"></param>
/// <param name="message">异常消息</param>
public static void Warn(Logger logger, Exception throwable,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Log(logger, LogLevel.Warn, throwable, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// 输出 INFO 日志
/// </summary>
/// <param name="logger"></param>
/// <param name="message">文本消息</param>
public static void Info(Logger logger, string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Log(logger, LogLevel.Info, message, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// 输出 INFO 日志
/// </summary>
/// <param name="logger"></param>
/// <param name="message">异常消息</param>
public static void Info(Logger logger, Exception throwable,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Log(logger, LogLevel.Info, throwable, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// 输出 DEBUG 日志
/// </summary>
/// <param name="logger"></param>
/// <param name="message">文本消息</param>
public static void Debug(Logger logger, string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Log(logger, LogLevel.Debug, message, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// 输出 DEBUG 日志
/// </summary>
/// <param name="logger"></param>
/// <param name="message">异常消息</param>
public static void Debug(Logger logger, Exception throwable,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Log(logger, LogLevel.Debug, throwable, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// 输出 TRACE 日志
/// </summary>
/// <param name="logger"></param>
/// <param name="message">文本消息</param>
public static void Trace(Logger logger, string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Log(logger, LogLevel.Trace, message, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// 输出 TRACE 日志
/// </summary>
/// <param name="logger"></param>
/// <param name="message">异常消息</param>
public static void Trace(Logger logger, Exception throwable,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Log(logger, LogLevel.Trace, throwable, memberName, sourceFilePath, sourceLineNumber);
}
// --------------------------------------------------------------------------------------------
/// <summary>
/// 日志输出
/// </summary>
/// <param name="logger"></param>
/// <param name="level">日志级别</param>
/// <param name="message">文本消息</param>
/// <param name="memberName">方法名</param>
/// <param name="sourceFilePath">文件</param>
/// <param name="sourceLineNumber">行号</param>
private static void Log(Logger logger, LogLevel level, string message,
string memberName, string sourceFilePath, int sourceLineNumber)
{
if (logger.IsEnabled(level))
{
// logger.Log(level, "[SEGMENT4| {}] [SEGMENT5| _] [SEGMENT6| {}]", caller, message);
var caller = GetCaller(memberName, sourceFilePath, sourceLineNumber);
logger.Log(level, "[{0}] [_] [{1}]", caller, message);
}
}
/// <summary>
/// 日志输出
/// </summary>
/// <param name="logger"></param>
/// <param name="level">日志级别</param>
/// <param name="throwable">异常消息</param>
/// <param name="memberName">方法名</param>
/// <param name="sourceFilePath">文件</param>
/// <param name="sourceLineNumber">行号</param>
private static void Log(Logger logger, LogLevel level, Exception throwable,
string memberName, string sourceFilePath, int sourceLineNumber)
{
if (logger.IsEnabled(level))
{
// logger.Log(level, "[SEGMENT4| {}] [SEGMENT5| _] [SEGMENT6| {}]", caller, message);
var caller = GetCaller(memberName, sourceFilePath, sourceLineNumber);
var message = GetExceptionInfo(throwable);
logger.Log(level, "[{0}] [_] [{1}]", caller, message);
}
}
/// <summary>
/// 获取调用者信息
/// </summary>
/// <param name="memberName">方法名</param>
/// <param name="sourceFilePath">文件</param>
/// <param name="sourceLineNumber">行号</param>
/// <returns></returns>
private static string GetCaller(string memberName, string sourceFilePath, int sourceLineNumber)
{
var source = string.IsNullOrEmpty(sourceFilePath)
? string.Empty
: sourceFilePath.Substring(sourceFilePath.LastIndexOf('\') + 1);
return $"{memberName ?? string.Empty}({source}:{sourceLineNumber})";
}
/// <summary>
/// 获取详细的异常描述
/// </summary>
/// <param name="ex">Exception</param>
/// <returns></returns>
private static string GetExceptionInfo(Exception ex)
{
if (ex == null) return string.Empty;
var message = new StringBuilder(ex.Message);
while (ex.InnerException != null)
{
ex = ex.InnerException;
message.AppendFormat(" => {0}", ex.ToString());
}
return message.ToString();
}
}