.Net Standard上的Log4Net没有用于获取没有存储库的记录器的版本 [英] Log4Net on .Net Standard has no version for getting logger without repository

查看:192
本文介绍了.Net Standard上的Log4Net没有用于获取没有存储库的记录器的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将项目移至.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆