4

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

Ray
  • 187,153
  • 97
  • 222
  • 204
Amjid Qureshi
  • 577
  • 1
  • 8
  • 19

2 Answers2

3

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"

Community
  • 1
  • 1
Ray
  • 187,153
  • 97
  • 222
  • 204
  • An alternative to this that works for me is to change the build action to *content* and then the log will always get created. But I am working in Visual Studio 2015 and it's currently 2017 when I am writing this, so who knows... – Snoop Feb 03 '17 at 16:08
2

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?)

Community
  • 1
  • 1
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • I ended up calling log4net.Config.XmlConfigurator.ConfigureAndWatch(log4netConfigFile); but you can also call it without a param adn it will look in your app.config file – Amjid Qureshi Sep 08 '10 at 05:21