我试图在。net 6.0控制台应用程序中配置EventLogAppender,我使用Log4Net 2.0.14,我得到以下异常…
log4net:ERROR Could not create Appender [EventLogAppender] of type [log4net.Appender.EventLogAppender]. Reported error follows.
System.TypeLoadException: Could not load type [log4net.Appender.EventLogAppender]. Tried assembly [log4net, Version=2.0.14.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a] and all loaded assemblies
at log4net.Util.SystemInfo.GetTypeFromString(Assembly relativeAssembly, String typeName, Boolean throwOnError, Boolean ignoreCase)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement)
log4net:ERROR Appender named [EventLogAppender] not found.
这是我的appender:
<log4net>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="Application" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ERROR"/>
<appender-ref ref="EventLogAppender"/>
</root>
</log4net>
查看EventLogAppender官方文档:
请注意这个appender不是本地log4net dotnet标准,因为它将在windows为非windows消费者依赖关系。如果您仍然需要netstandard目标的EventLog日志记录,请参阅https://www.nuget.org/packages/log4net.appenders.netcore获取替代方案
所以使用Nuget包"Log4Net.Appenders.NetCore"
追加器声明略有不同:
<appender name="EventLogAppender" type="Log4Net.Appenders.NetCore.EventLogAppender,Log4Net.Appenders.NetCore">