實現目標:將log4net的相關操作封裝成一個 .Net Standard類庫 demo地址:https://github.com/PuzzledAlien/log4net_demo/tree/master/DotNetCoreConsole_V2 Step 1 建立解決方案和處理項目相關配置 在當 ...
實現目標:將log4net的相關操作封裝成一個 .Net Standard類庫
demo地址:https://github.com/PuzzledAlien/log4net_demo/tree/master/DotNetCoreConsole_V2
Step 1 建立解決方案和處理項目相關配置
在當前解決方案下新建項目,選擇 .Net Standard類庫,命名“MyLogManager”
nuget引用log4net,添加log4net.config配置,右鍵點擊log4net.config,選擇“屬性”,修改log4net.config文件屬性
剛添加的log4net.config文件屬性可能如下圖所示(生成操作為無,不複製到輸出目錄)
需要修改成如下所示(生成操作 無 改為 內容
,複製到輸出目錄 - 選擇 如果較新則複製
)
重新生成解決方案後,在生成目錄中就能看到log4net.config文件
Step 2 上代碼
思路:兩個StartLogger方法,在程式啟動時啟動該方法,一個方法自己配置處理,一個走預設
代碼如下
public class MyLogManager
{
private static ILoggerRepository _loggerRepository;
/// <summary>
/// 啟動logger
/// </summary>
/// <param name="repository">repository名稱</param>
/// <param name="fileName">配置文件名稱</param>
public static void StartLogger(string repository, string fileName)
{
_loggerRepository = LogManager.CreateRepository(repository);
XmlConfigurator.ConfigureAndWatch(_loggerRepository, new FileInfo(fileName));
}
/// <summary>
/// 啟動logger
/// </summary>
public static void StartLogger()
{
_loggerRepository = LogManager.CreateRepository(nameof(MyLogManager));
XmlConfigurator.ConfigureAndWatch(_loggerRepository, new FileInfo("log4net.config"));
}
public static ILog GetMyLog<T>(T t)
{
return LogManager.GetLogger(_loggerRepository.Name, t.GetType());
}
public static ILog GetMyLog(object obj)
{
return LogManager.GetLogger(_loggerRepository.Name, obj.GetType());
}
public static ILog GetMyLog(Type type)
{
return LogManager.GetLogger(_loggerRepository.Name, type);
}
}
封裝之後,調用代碼會比第一版本的稍微精簡
static void TestV1()
{
MyLogManager.MyLogManager.StartLogger("DotNetCoreConsole_V2", "log4net.config");
var log = MyLogManager.MyLogManager.GetMyLog(typeof(Program));
const int numberOfCycles = 20000;
var sw = Stopwatch.StartNew();
for (var i = 0; i < numberOfCycles; i++)
{
log.InfoFormat("testNum: {0} ", i);
}
sw.Stop();
Console.WriteLine();
Console.WriteLine("Ellapsed: {0}, numPerSec: {1}", sw.ElapsedMilliseconds, numberOfCycles / (sw.ElapsedMilliseconds / (double)1000));
Console.ReadKey();
}
代碼重構和優化
思考:可不可以去掉StartLogger這行代碼呢?可以。
實現如下
public class NullLogManager
{
private static ILoggerRepository _loggerRepository;
private static ILoggerRepository LoggerRepository
{
get
{
if (_loggerRepository != null)
{
return _loggerRepository;
}
_loggerRepository = LogManager.CreateRepository(nameof(NullLogManager));
XmlConfigurator.ConfigureAndWatch(_loggerRepository, new FileInfo("log4net.config"));
return _loggerRepository;
}
}
public static ILog GetMyLog<T>(T t)
{
return LogManager.GetLogger(LoggerRepository.Name, t.GetType());
}
public static ILog GetMyLog(object obj)
{
return LogManager.GetLogger(LoggerRepository.Name, obj.GetType());
}
public static ILog GetMyLog(Type type)
{
return LogManager.GetLogger(LoggerRepository.Name, type);
}
public static ILog GetMyLog()
{
return LogManager.GetLogger(LoggerRepository.Name, nameof(GetMyLog));
}
}
調用代碼可再次精簡
static void TestNullLogManager()
{
var log = MyLogManager.NullLogManager.GetMyLog(typeof(Program));
const int numberOfCycles = 20000;
var sw = Stopwatch.StartNew();
for (var i = 0; i < numberOfCycles; i++)
{
log.InfoFormat("testNum: {0} ", i);
}
sw.Stop();
Console.WriteLine();
Console.WriteLine("Ellapsed: {0}, numPerSec: {1}", sw.ElapsedMilliseconds, numberOfCycles / (sw.ElapsedMilliseconds / (double)1000));
Console.ReadKey();
}