log4net doesn't log in Windows Event Viewer

夙愿已清 提交于 2019-11-30 21:23:11

You need to call configure.

Change:

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

To

[assembly: log4net.Config.XmlConfigurator(Watch = true)] 

When you specify ConfigFile = "App.config" its going to look for App.config but your filename would be [FileName].Config.

You need to call XmlConfigurator.Configure from the log4net library to initialize it. (see below)

class Program
{
    static void Main(string[] args)
    {
        // you need this
        XmlConfigurator.Configure();
        log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
        log.Error("test error", new Exception("error's exception", new Exception("error's innerexception")));

        Console.Read();
    }
}

Call XmlConfigurator.Configure() at the begining of your App.

You also need to grant the user running the application rights to put data in the eventlog.

A good way to do this is with powershell, admin mode

  New-EventLog EventLogName -source ApplicationName

Also, add this two parameters into the appender

  <param name="LogName" value="EventLogName " />
  <param name="ApplicationName" value="ApplicationName" />

Regards,

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