using NLog;
using NLog.Config;
using NLog.Targets;
namespace Mirle.Component.Record
{
///
/// 記錄檔類別
///
public class LogWrapper
{
///
/// 建構式
///
private LogWrapper() { }
///
/// 關聯式資料庫物件
///
private static volatile LogWrapper _logFile;
///
/// 物件鎖定
///
private static readonly object _syncRoot = new object();
///
/// 記錄設定檔
///
private static readonly LoggingConfiguration _loggingConfiguration = new LoggingConfiguration();
///
/// 紀錄檔實例
///
public static LogWrapper Instance
{
get
{
if (_logFile == null)
{
lock (_syncRoot) // 鎖定避免多執行緒重覆呼叫建立物件
{
_logFile = new LogWrapper();
}
}
return _logFile;
}
}
///
/// 取得記錄器
///
/// 紀錄器名稱
/// 紀錄器設定檔
/// 紀錄器物件
public Logger GetLogger(string logger, FileTarget fileTarget)
{
_loggingConfiguration.AddRuleForAllLevels(fileTarget, logger);
LogManager.Configuration = _loggingConfiguration;
return LogManager.GetLogger(logger);
}
}
}