Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net on .Net Standard has no version for getting logger without repository

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);

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?

like image 543
John Demetriou Avatar asked Dec 21 '25 21:12

John Demetriou


1 Answers

The implementation of GetLogger is simple:

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);

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.

like image 66
Jon Skeet Avatar answered Dec 24 '25 10:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!