2

Please help me what am missing in writing log for the exception. Always am getting an empty file

Logger in Sitecore.Config

<?xml version="1.0" encoding="UTF-8"?>
<log4net>
   <appender name="CustomLogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
      <file value="$(dataFolder)/logs/Custom/CustomLog.log" />
      <encoding value="utf-8" />
      <staticLogFileName value="false" />
      <datePattern value="_yyyyMMdd" />
      <rollingStyle value="Date" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <appendToFile value="true" />
      <preserveLogFileNameExtension value="true" />
      <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="%d (%-5p) %n%m%n%n" />
      </layout>
   </appender>
   <root>
      <priority value="INFO" />
      <appender-ref ref="LogFileAppender" />
   </root>
   <logger name="CustomLog" additivity="false">
      <priority value="INFO" />
      <appender-ref ref="CustomLogFileAppender" />
   </logger>
</log4net>

Code:

ILog Logger = LogManager.GetLogger("CustomLog");
try
{ }
catch(Exception e)
{
    Logger.Info(e);
}
  • Can you share the full code? Based on what you've provided above, if your try block is empty then it will never log anything since no exception occurs. – Paul Jul 5 '18 at 22:34
6

Two possible problems that could cause this i can think of:

  1. You are referencing log4net.dll instead of Sitecore.Logging.dll, You need to reference Sitecore.Logging.dll in your project for Sitecore custom logger to work
  2. You custom log4net configurations lives outside <sitecore> node, Make sure it is located within that node:

Hope this helps

1

I think that

 <priority value="INFO" />

Should be changed to

<level value="INFO"/>

Give that a go

1

For logging exception you should never use Info method. You should use Warn, Error or Fatal and pass the exception to that method. Thanks to that, nice stack trace will be populated in the log file, e.g.:

catch(Exception e)
{
  Log.Warn("Some meaningful description", e, "CustomLog");
}

Also make sure that you use what @JKerny wrote in his answer: use level instead of priority for log level value.

0

Try it.

<sc.variable name="logLevel" set:value="INFO"/>
<log4net>
  <appender name="CustomLogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
    <file value="$(dataFolder)/logs/log.{date}.txt" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%5t %d{yyyy-MM-dd HH:mm:ss.fff} %-5p %c - %m%n" />
    </layout>
  </appender>
  <root>
    <priority set:value="$(logLevel)" />
  </root>
  <logger name="CustomLog" additivity="false">
    <level value="$(logLevel)" />
    <appender-ref ref="CustomLogFileAppender" />
  </logger>
</log4net>

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.