C# windows event viewer

帅比萌擦擦* 提交于 2021-02-10 06:24:13

问题


I want to log the errors from my C# app in Windows Event Viewer using log4net with the EventLogAppender (the errors must be logged under the Application log) I have a log4net.config class with this code

<configuration>
<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" />
</configSections>
  <log4net>
   <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
       <applicationName value="MySource" />
       <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="{%level} {%date} – %message%newline" />
       </layout>
    </appender>

  <root>
     <level value="DEBUG" />
     <appender-ref ref="EventLogAppender" />
  </root>
  </log4net>
</configuration>

In AssemblyInfo I added the following line

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch=true)]

Then in my Program class

class Program
{
  static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

  static void Main(string[] args)
  {
     string name=null;
     try
     {
         Console.WriteLine("Name : " + name.ToString());
     }
     catch (NullReferenceException nullException)
     {
        log.Error("Name is NULL", nullException);
     }
}
}

I have to mention that I used power shell to create a new EventLog called "MySource"

The problem is that I receive the following error:

log4net:ERROR Could not create Appender [EventLogAppender] of type [log4net.Appender.EventLogAppender]. Reported error follows.
System.TypeLoadException: Could not load type 'log4net.Appender.EventLogAppender' from assembly 'log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'.
   at System.Reflection.RuntimeAssembly.GetType(QCallAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type, ObjectHandleOnStack keepAlive, ObjectHandleOnStack assemblyLoadContext)
   at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
   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.

回答1:


Latest version of log4net (2.0.8 as of today) is implementing .net standard 1.3: https://logging.apache.org/log4net/release/release-notes.html.

In .net standard 1.3 EventLogAppender is not supported: https://logging.apache.org/log4net/release/framework-support.html#netstandard-1.3

If you want to use EventLogAppender, you will have to switch to .net framework.



来源:https://stackoverflow.com/questions/61639853/c-sharp-windows-event-viewer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!