我有一个类库,它执行安装所需的例程。从我所研究的一切来看,我相信我已经正确地安排了事情。但是,例程绝对不会记录任何内容。以下是我所拥有的:
-
我添加了类库项目作为对宿主项目的引用。
-
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public InstallDLL() {
Log.Error("Where are you goinng?");
}
-
在宿主应用程序中,我向AssemblyInfo.cs类添加了以下行:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
-
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="TestAppender" type="log4net.Appender.RollingFileAppender" >
<file value=".\Log Directory\MyTestAppender.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="2MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%M %C] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<!-- If the following line is not included the log file will not be created even if log4net is configured with this file. -->
<appender-ref ref="TestAppender" />
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
我肯定错过了什么,但是什么?请记住,从主机应用程序进行日志记录可以很好地工作。。。
我想我可能知道这里发生了什么。此项目生成的.dll文件在宿主项目运行之前运行,因为它是安装脚本的一部分。我认为主机项目必须首先运行以初始化log4net配置。当我从宿主应用程序而不是从安装程序包调用.dll文件中的方法时,log4net日志工作得很好。有人知道这是不是根本原因吗?我该怎么解决?