问题
I trying to migrate a c# project from .net framework v4.6 to .net standard. The project has log4net v2.0.8 dependency.
I found this SO anwser, which recommends to use .net standard 1.3 and gives reference to this post to get more detailed solution.
The problem occurs when configuring log4net with XmlConfigurator.Configure
method, which requires ILoggerRepository
as the first argument.
In the post LogManager.GetRepository(Assembly.GetEntryAssembly())
method used, but Assembly.GetEntryAssembly()
is not supported in .net standard 1.3.
Official documentation is also broken, because XmlConfigurator.Configure
method signature and it's example usage doesn't match.
So, how can I configure log4net in .net standard 1.3 project?
回答1:
In your .NET Standard 1.3 class library project, provide an Assembly
argument in the signature of the method taking care of the Log4net
configuration, eg:
public static void Configure(Assembly assembly)
{
ILoggerRepository repository = LogManager.GetRepository(assembly);
XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
// ...
}
Call this method from your actual application, being developed in either the full .NET Framework or .NET Core, passing in this Assembly
argument via eg: Assembly.GetEntryAssembly()
.
Assembly.GetEntryAssembly()
is supported in both the full .NET Framework and .NET Core.
来源:https://stackoverflow.com/questions/53500512/log4net-configuration-in-net-standard-1-3-project