我查了一下:
log4net with EventLogAppender doesn't log
尝试与该问题中的答案几乎相同,但没有成功,而且我还以管理员身份运行Visual Studio。
.config文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<appSettings>
<add key="log4net.Internal.Debug" value="true" />
</appSettings>
<log4net>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.Patternlayout">
<conversionPattern value="%date %-5level %logger - %message%newline "/>
</layout>
<!--<level value="ALL"/>-->
<threshhold value="ALL"></threshhold>
<!--<param name="Threshold" value="ERROR" />-->
<!--<logName value="Services"/>-->
<applicationName value="SikuliTestResultImporter"/>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="EventLogAppender"/>
</root>
</log4net>
</configuration>
我尝试做这样的事情:
private static readonly ILog log = LogManager.GetLogger("EventLogAppender");
try
{
throw new Exception("Exception mon!");
}
catch (Exception ex)
{
log.Error(ex.Message);
}
我在.NET 2中使用log4net程序集,如果这与任何事情有关。
我只是得到.config不是有效的xml文件的错误:
log4net:ERROR读取ConfigurationSettings时出现异常。校验 你的.config文件是格式良好的XML。
答案 0 :(得分:1)
试试这个:(首先是configSections,除非你得到了你所描述的例外)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
<appSettings>
<add key="log4net.Internal.Debug" value="true" />
</appSettings>
<log4net>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.Patternlayout">
<conversionPattern value="%date %-5level %logger - %message%newline "/>
</layout>
<!--<level value="ALL"/>-->
<threshhold value="ALL"></threshhold>
<!--<param name="Threshold" value="ERROR" />-->
<!--<logName value="Services"/>-->
<applicationName value="SikuliTestResultImporter"/>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="EventLogAppender"/>
</root>
</log4net>
</configuration>