6

我正在设置一个 dll 以用作不同应用程序的第三方 dll。我希望这个 dll 拥有自己的日志记录,因此外部应用程序不必处理任何设置(我不相信他们使用与我们相同的日志记录)。我读过这可能不是最好的解决方案,但这是我被赋予的任务。我们想用这个来使用 log4net。我在这里查看了其他一些问题,他们提到它可以通过代码进行配置,但是,我遇到的主要问题是我们的代码中没有明确的入口点来配置 log4net。我很好奇我是否应该放弃让dll自行配置并拥有一个由配置dll日志记录的辅助应用程序调用的方法,或者是否有更好的方法来解决这个问题。

4

1 回答 1

10

您可以通过编程方式配置 log4net。也许将此代码添加到 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);
}
于 2014-06-02T16:03:55.470 回答