初始化
This commit is contained in:
61
Face.Log4Net/LoggerFactory.cs
Normal file
61
Face.Log4Net/LoggerFactory.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using log4net;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.Log4Net
|
||||
{
|
||||
/// <summary>
|
||||
/// Logger 工厂
|
||||
/// </summary>
|
||||
class LoggerFactory
|
||||
{
|
||||
private static Dictionary<string, ILog> dicLog = new Dictionary<string, ILog>();
|
||||
|
||||
/// <summary>
|
||||
/// 创建实例 ,默认读取App_Data 下的log4net.config文件
|
||||
/// </summary>
|
||||
/// <param name="loggerName"></param>
|
||||
/// <returns></returns>
|
||||
public static ILog CreateInstance(string loggerName)
|
||||
{
|
||||
return CreateInstance(loggerName, null);
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建实例
|
||||
/// </summary>
|
||||
/// <param name="loggerName"></param>
|
||||
/// <param name="configFilename">log4net.config文件路径</param>
|
||||
/// <returns></returns>
|
||||
public static ILog CreateInstance(string loggerName, string configFilename)
|
||||
{
|
||||
if (!dicLog.ContainsKey(loggerName))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(configFilename))
|
||||
{
|
||||
configFilename = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).FullName + "\\App_Data\\configs\\log4net.config";
|
||||
}
|
||||
|
||||
log4net.Config.XmlConfigurator.Configure(new FileInfo(configFilename));
|
||||
//从配置文件中读取Logger对象
|
||||
//WebLogger 里面的配置信息是用来将日志录入到数据库的
|
||||
//做为扩展 做判断来确定日志的记录形式,数据库也好,txt文档也好,控制台程序也好。
|
||||
var log = log4net.LogManager.GetLogger(loggerName);
|
||||
|
||||
//防止并发出错
|
||||
if (!dicLog.ContainsKey(loggerName))
|
||||
{
|
||||
dicLog.Add(loggerName, log);
|
||||
}
|
||||
return log;
|
||||
}
|
||||
else
|
||||
{
|
||||
return dicLog[loggerName];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user