Reputation: 1118
I use log4net for logging to a text file and to the Windows Event Log. Both work fine when I use the F5 run debug method in Visual Studio. But as soon as I run the exe directly it does not log to the Event Log any more. No errors are thrown and the text file logging still works fine.
Now I found out that the problem was, namely that VS is always started as administrator but the exe file not, when run directly. From what I've seen there is at least an exception when log4net cannot log to the Event Log due to missing permissions, but not in my case. It is as if there was no log statement.
Is there a way to have log4net log to the Event Log without being started as administrator?
The config:
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
<file value="c:\\mylogfile.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="FileAppender" />
<appender-ref ref="EventLogAppender" />
</root>
Upvotes: 0
Views: 883
Reputation: 16067
Try running your exe once as admin and then try it as normal and it will probably work.
You need admin rights to create an EventSource, but not to write to the event log.
Because you have not set ApplicationName in your EventLogAppender, the event source will be based on the name of the program which is app.exe or app.vshost.exe depending on whether or not you are running from outside or inside VS.
So, if you run your exe once as admin,it will then be able to create the eventsource and from then on, you should be able to write to the event log even when running in non-admin mode.
If your program is going to be run on other computers, you may need to create the event source during the install procedure.
Upvotes: 3