.Net Standard 上的 Log4Net 没有用于在没有存储库的情况下获取记录器的版本 [英] Log4Net on .Net Standard has no version for getting logger without repository
问题描述
我正在尝试将项目移至 .net 标准,它基本上是一个类库.我正在使用 log4net 进行日志记录,我正在使用该方法
I am trying to move a project to .net standard and it basically is a class library. I am using log4net for logging and I was using the method
public static ILog GetLogger(string name);
由于这是一个被多个项目引用的库,定义存储库不应该发生,因为每个项目都引用这个,定义它自己的存储库以进行日志记录.在移植到 .Net 标准时如何规避这个问题?
As this is a library referenced by multiple projects, defining repository should not happen as each project referencing this one, defines it's own repository for logging. How can I circumvent this issue when it comes to porting to .Net standard?
推荐答案
GetLogger
的实现很简单:
public static ILog GetLogger(string name)
{
return GetLogger(Assembly.GetCallingAssembly(), name);
}
假设您想使用类库的程序集,您可以轻松地编写自己的辅助方法:
Assuming you want to use your class library's assembly, you can write your own helper method for that easily:
private static ILog GetLogger(string name) =>
LogManager.GetLogger(typeof(SomeTypeInYourLibrary).Assembly, name);
它基本上是将程序集硬编码为包含您指定类型的程序集",这就是您始终从库中调用 GetLogger(string)
获得的结果.
It's basically hard-coding the assembly to be "the assembly containing the type you've specified" which is what you'd always get from calling GetLogger(string)
from your library anyway.
这篇关于.Net Standard 上的 Log4Net 没有用于在没有存储库的情况下获取记录器的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!