using log4net;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace LB_SmartVisionCommon
|
{
|
/// <summary>
|
/// 同步记录日志类
|
/// </summary>
|
public static class LogHelper
|
{
|
static string LineT = "记录信息:";
|
static readonly ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
/// <summary>
|
/// Info类型 - 异步记录
|
/// </summary>
|
/// <param name="message">记录信息</param>
|
public static void Info(string message)
|
{
|
Task.Factory.StartNew(() =>
|
{
|
try
|
{
|
_logger.Info(LineT + message);
|
}
|
catch (Exception ex)
|
{
|
// 异步日志记录失败时的处理
|
System.Diagnostics.Debug.WriteLine($"日志记录失败: {ex.Message}");
|
}
|
}, TaskCreationOptions.LongRunning);
|
}
|
|
/// <summary>
|
/// Debug类型 - 异步记录
|
/// </summary>
|
/// <param name="message">记录信息</param>
|
public static void Debug(string message)
|
{
|
Task.Factory.StartNew(() =>
|
{
|
try
|
{
|
_logger.Debug(LineT + message);
|
}
|
catch (Exception ex)
|
{
|
System.Diagnostics.Debug.WriteLine($"日志记录失败: {ex.Message}");
|
}
|
}, TaskCreationOptions.LongRunning);
|
}
|
|
/// <summary>
|
/// Warn类型 - 异步记录
|
/// </summary>
|
/// <param name="message">记录信息</param>
|
public static void Warn(string message)
|
{
|
Task.Factory.StartNew(() =>
|
{
|
try
|
{
|
_logger.Warn(LineT + message);
|
}
|
catch (Exception ex)
|
{
|
System.Diagnostics.Debug.WriteLine($"日志记录失败: {ex.Message}");
|
}
|
}, TaskCreationOptions.LongRunning);
|
}
|
|
/// <summary>
|
/// Error类型 - 异步记录
|
/// </summary>
|
/// <param name="message">记录信息</param>
|
public static void Error(string message)
|
{
|
Task.Factory.StartNew(() =>
|
{
|
try
|
{
|
_logger.Error(LineT + message);
|
}
|
catch (Exception ex)
|
{
|
System.Diagnostics.Debug.WriteLine($"日志记录失败: {ex.Message}");
|
}
|
}, TaskCreationOptions.LongRunning);
|
}
|
|
/// <summary>
|
/// Error类型 - 异步记录异常
|
/// </summary>
|
/// <param name="message">记录信息</param>
|
/// <param name="exception">异常对象</param>
|
public static void Error(string message, Exception exception)
|
{
|
Task.Factory.StartNew(() =>
|
{
|
try
|
{
|
_logger.Error(LineT + message, exception);
|
}
|
catch (Exception ex)
|
{
|
System.Diagnostics.Debug.WriteLine($"日志记录失败: {ex.Message}");
|
}
|
}, TaskCreationOptions.LongRunning);
|
}
|
}
|
}
|