Fjodr
Fjodr

Reputation: 923

log4net EventLog custom logname created, but not used

In a project, i'm using log4net to write down in the event log. I need to create a custom log so all will go there. I have tried to add the event to different logs, and it worked, but for some reasons, now it is impossible to change the logname from the config file. Even if I change it, it will keep the previous one and log into it. Here is a sample of my app.config:

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{yyyy-MMMM-dd HH:mm:ss, fff} [%thread] %level %message %exception%newline"/>
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="DEBUG" />
    <levelMax value="FATAL" />
  </filter>
  <ApplicationName>TestLoggerFichierConfig</ApplicationName>
  <LogName>AgentLogger</LogName>
</appender>

I read that the event log must be restarted when adding a new log so the recent events will be displayed in it. I did this, and it allowed me to see the new log that has been created, but nothing will go there. Is it possible to force Log4Net to stop using the previous log to rather use the one I defined in the app.config?

Thanks for the help.

Update:
It seems that le EventLogs recognizes the source application and decide in wich log to put the new events since he remembers where it previously went. When I first set the logName, it works fine. If I stop the app, change the logname and restart it, the event will still go to the previous log. If the logName does not exists in the event log, it will be created, but not used! There might be something to do, but it is not in the side of log4net, and it may be dangerous to change the windows settings at this level. I created two eventLogAppender on the same app.config file, both pointing at different logs. the events got to the same log though. It is being obvious that the problem doesn't come from log4net, and the solution to my problem will not be solved by code. Thanks for the great advices though.

Upvotes: 1

Views: 2580

Answers (3)

Ed L
Ed L

Reputation: 11

I ran into this and it wasn't down to log4net but the re-mapping of the source to a new custom log. I took log4net out of the equation and finding that the problem occurred usint the EventLog class directly, I eventually found EventLog.CreateEventSource is not creating a custom log

I think it's also the reason behind this Windows service always writes to custom log and application log

Upvotes: 1

Killnine
Killnine

Reputation: 5880

First and foremost, I'd encourage you to turn on internal debugging, as found in the link ntcolonel posted. Add this to your config file:

<add key="log4net.Internal.Debug" value="true"/>

Perhaps I am not understanding your question entirely, but I'd like to ensure you have both created the appender and properly tell log4net that you want to actually use that appender:

  <log4net debug="true">
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="Logs\SomeApplication.xml"/>
  <appendToFile value="true"/>
  <rollingStyle value="Size"/>
  <countDirection value="1"/>
  <maxSizeRollBackups value="30"/>
  <maximumFileSize value="10MB"/>
  <staticLogFileName value="true"/>
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
    <locationInfo value="true"/>
  </layout>
</appender>

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  <to value="[email protected]"/>
  <from value="[email protected]"/>
  <subject type="log4net.Util.PatternString" value="Some Application Error - %property{log4net:HostName}"/>
  <smtpHost value="smtp.somehost.com"/>
  <bufferSize value="1"/>
  <threshold value="ERROR"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %property{log4net:HostName} %logger %level %newline%newline%property{ExceptionThrottleInformation}%newline%newline%message%newline"/>
  </layout>
  <filter type="SomeNamespace.SomeSubNamespace.Log4Net.ExceptionThrottleFilter, SomeSubNamespace">
    <threshold value="10"/>
    <thresholdTimeoutSeconds value="60"/>
    <timeoutSecondsBetweenMessages value="600000"/>
    <exceptionText value="Timeout expired"/>
  </filter>
</appender>

<appender name="DatabaseAppender" type="SomeNamespace.SomeSubNamespace.DatabaseTraceAppender, SomeSubNamespace">
  <hoursToKeepInformationalTraces value="48"/>
  <hoursToKeepErrorTraces value="96"/>
  <threshold value="INFO"/>
</appender>

<root>
  <level value="INFO"/>
  <appender-ref ref="RollingFileAppender"/>
  <appender-ref ref="SmtpAppender"/>
  <appender-ref ref="DatabaseAppender"/>
</root>

Note that while I have multiple appenders, I reference which to call within the root tag. Since you didn't post your whole config file, it's tough to tell if everything is matching as it should.

Upvotes: 0

ntcolonel
ntcolonel

Reputation: 900

Something is probably wrong with your configuration and it is simply not telling you about it because log4net itself is designed to never, ever throw errors.

You can turn on internal debugging for log4net, but don't deploy to production with log4net debugging turned on. Weirdness will eventually happen if deploy your product with the log4net debugging switch on.

http://haacked.com/archive/2006/09/27/Log4Net_Troubleshooting.aspx

http://logging.apache.org/log4net/release/faq.html (see the troubleshooting section)

Upvotes: 1

Related Questions