Reputation: 602
So I've got a website and a console app that runs daily.
They both call a Function called ProcessIncident(). The website allows you to do it manually for an individual incident and the console app does a batch every night.
Within the function I have various log4net Log.InfoFormat() and Log.DebugFormat() calls
When I run from the website it logs fine
when I run from the console app it doesn't log at all
The path specified definatly exists
The console app config is as follows
<?xml version="1.0" encoding="utf-8" ?>
<log4net xmlns="urn:log4net">
<logger name="NHibernate">
<level value="OFF" />
<appender-ref ref="NHibernateFileAppender" />
</logger>
<logger name="SMS">
<level value="ALL" />
<appender-ref ref="SmsFileAppender" />
</logger>
<appender name="SmsFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:\XXXX\SMS.IncidentBilling.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date] %-5level %logger %message %newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<appender name="NHibernateFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="D:\Dev\SMS\Main\Source\SMS.Website\Logs\nhibernate.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date] %appdomain %-5level %c %message %newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
</log4net>
In the console app I have refereneced log4net dll and am declareing amember as
private ILog Log = LogManager.GetLogger(typeof(Task));
Can anyone see anything stupid that I am doing. THere are no actual errors, just nothing is gettign logged via the console app
Upvotes: 4
Views: 2584
Reputation: 192186
Along with Leniel Macaferi's answer, one difference to note with a web app versus a console app is that if you are using a separate custom config file for log4net (i.e. it is not part of the App.config file) then you need to set the file's property to be copied when it is built and compiled. Otherwise, it will not find it. This typically is not a problem with a web application since it can find the file within the root directory.
To change it, in Visual Studio:
Find the config file in Solution Explorer -> right-click the file -> select Properties -> Copy to Output Directory -> "Copy always"
Upvotes: 3
Reputation: 102378
I don't remember exactly what I did, but I think you should initialize log4net before using it.
Something like this line:
log4net.Config.XmlConfigurator.Configure();
Take a look here for more info:
Have log4net use application config file for configuration data.
See this one too (he's calling log4net.Config.BasicConfigurator.Configure();
) :
Log4Net Tutorial in C# .net (How can I show log in a file?)
Upvotes: 2