在dll中配置log4net日志记录 [英] Configure log4net logging in dll
问题描述
我正在设置一个dll,作为不同应用程序的第三方dll。我想要这个dll有自己的日志记录,所以外部应用程序不必处理任何设置(我不相信他们使用相同的日志记录,因为我们这样做)。我已经阅读过,可能不是最好的解决方案,但这是我给出的任务。我们想用这个log4net。我看过这里的其他几个问题,他们提到可以通过代码进行配置,但是我遇到的主要问题是我们的代码中没有明确的切入点来配置log4net。我很好奇,如果我应该放弃dll配置自己,并有一个方法,由辅助应用程序调用,配置dll的日志记录,或者如果有更好的方法来解决这个问题。任何输入都将非常感谢
I'm setting up a dll to be used as a third party dll for a different application. I want this dll to have it's own logging so the external application doesn't have to deal with setting up anything (I don't believe they use the same logging as we do). I've read that may not be the best solution but it's the task I've been given. We want to use log4net with this. I've looked at a few of the other questions on here and they mention that it is configurable via code, however, the main issue I'm having is that there is no clear cut entry point into our code to configure log4net. I'm curious if I should just abandon having the dll configure itself and have a method that is called by the secondary application that configures the dll's logging or if there is a better way to go about this. Any input would be much appreciated
推荐答案
您可以通过编程方式配置log4net。也许将这段代码添加到你的DLL的构造函数中。
You can configure log4net programmatically. Perhaps add this code to the constructor of your DLL.
if (!log4net.LogManager.GetRepository().Configured)
{
// my DLL is referenced by web service applications to log SOAP requests before
// execution is passed to the web method itself, so I load the log4net.config
// file that resides in the web application root folder
var configFileDirectory = (new DirectoryInfo(TraceExtension.AssemblyDirectory)).Parent; // not the bin folder but up one level
var configFile = new FileInfo(configFileDirectory.FullName + "\\log4net.config");
if (!configFile.Exists)
{
throw new FileLoadException(String.Format("The configuration file {0} does not exist", configFile));
}
log4net.Config.XmlConfigurator.Configure(configFile);
}
这篇关于在dll中配置log4net日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!