0

I'm trying to create a custom log for my application, but for some reason, it is not working when I deployed it into the server. Locally it is working just fine.

I have already tried what's mentioned here but it didn't help: Sitecore custom log file is empty

I have also tried to remove the immediate flush, but that also didn't work. Could someone help me to understand what is going on?

Platform info:

  • Sitecore XP version 10.1
  • Server is a k8s pod on Azure AKS
  • .Net Framework 4.8

My configuration patch file that goes on {sitecoreXPInstace}/App_Config/Include/MYAPP/Foundation/MYAPP.Foundation.Logging.config

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<sitecore>
    <log4net>
        <appender name="MYAPP.Foundation.Logging.Appender"
            type="log4net.Appender.RollingFileAppender, Sitecore.Logging">
            <file value="$(dataFolder)/logs/main.{date}.txt" />
            <appendToFile value="true" />
            <rollingStyle value="Size" />
            <maxSizeRollBackups value="-1" />
            <maximumFileSize value="10MB" />
            <immediateFlush value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%d{yyyy/dd/MM} %d{ABSOLUTE} %-5p %m%n" />
            </layout>
            <encoding value="utf-8" />
        </appender>
        <appender name="IntegrationLogFileAppender"
            type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
            <file value="$(dataFolder)/logs/Integration.log.{date}.txt" />
            <appendToFile value="true" />
            <rollingStyle value="Size" />
            <maxSizeRollBackups value="-1" />
            <maximumFileSize value="10MB" />
            <!--<param name="ImmediateFlush" value="true" />-->
            <ImmediateFlush value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" />
            </layout>
        </appender>

        <logger name="MYAPP.Foundation.Logging.SCLogger" additivity="false">
            <level value="ALL" />
            <appender-ref ref="MYAPP.Foundation.Logging.Appender" />
            <appender-ref ref="IntegrationLogFileAppender" />
        </logger>
    </log4net>

    <services>
        <register serviceType="MYAPP.Foundation.Logging.SCLogger, MYAPP.Foundation.Logging"
            implementationType="MYAPP.Foundation.Logging.SCLogger, MYAPP.Foundation.Logging"
            resolve="true" scope="Transient" />
    </services>
</sitecore>

My Log implementation:

using System;
namespace Echopark.Foundation.Logging
{
    public class SCLogger
    {
        private static SCLogger _instance;
        private static readonly object _lock = new object();

        private const string LoggerName = "MYAPP.Foundation.Logging.SCLogger";
        //private static ILog _logger;

        private SCLogger() { }

        public static SCLogger Log
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            
                            _instance = new SCLogger();
                        }
                    }
                }
                return _instance;
            }
        }

        public void Error(Exception ex, string message, object owner)
        {
            Sitecore.Diagnostics.Log.Error(message, ex, owner);
        }

        public void Warning(string methodName, string message, object owner)
        {
            Sitecore.Diagnostics.Log.Warn(message, owner);
        }
    }
}

The log file that should not be empty:

enter image description here

UPDATE NOTE

I have tried to update the <level value="ALL" /> tag to <priority value="ALL" /> and this also didn't work.

Also, I just saw that I can't delete the log files from the log viewer tool. It says that my log has been used in another process. I have tried to restart the application, but I got the same thing with the difference that I got two logs files with the same name that didn't work.

enter image description here

UPDATE NOTE

I have updated my log to use log4net instead of Sitecore log4net, and it work for a while but after a few logs it have stopped again. It seems that the logs are get truncated.

New log file class:

  public class SCLogger
    {
        private static SCLogger _instance;

        private const string LoggerName = "Echopark.Foundation.Logging.SCLogger";
        private static ILog _logger;

        private SCLogger() { }

        public static SCLogger Log
        {
            get
            {
                _instance = new SCLogger();
                _logger = LogManager.GetLogger(LoggerName);

                return _instance;
            }
        }

        public void Error(Exception ex, string message, object owner)
        {
            if (string.IsNullOrEmpty(message)) throw new NullReferenceException(CustomLoggerResource.DefaultEmptyMessageError);

            _logger.Error(message, ex);
        }

        public void Warning(string methodName, string message, object owner)
        {
            if (string.IsNullOrEmpty(methodName) || string.IsNullOrEmpty(message)) throw new NullReferenceException(CustomLoggerResource.DefaultEmptyMessageError);

            _logger.Warn(message);
        }
}

New config patch:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
    <sitecore>
        <log4net>
            <appender name="Echopark.Foundation.Logging.IntegrationLogFileAppender"
                type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
                <file value="$(dataFolder)/logs/Integration.log.{date}.txt" />
                <appendToFile value="true" />
                <maximumFileSize value="500KB" />
                <maxSizeRollBackups value="2" />
                <threshold value="5"/>
                <param name="ImmediateFlush" value="true" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" />
                </layout>
            </appender>
            <logger name="Echopark.Foundation.Logging.SCLogger" additivity="false">
                <priority value="ALL" />
                <appender-ref ref="Echopark.Foundation.Logging.IntegrationLogFileAppender" />
            </logger>
        </log4net>
        <services>
            <register serviceType="Echopark.Foundation.Logging.SCLogger, Echopark.Foundation.Logging"
                implementationType="Echopark.Foundation.Logging.SCLogger, Echopark.Foundation.Logging"
                resolve="true" scope="Transient" />
        </services>
    </sitecore>
</configuration>
2
  • From showconfig.aspx, do you see your configs? Commented Jul 20, 2022 at 4:26
  • Yep, it's there. Commented Jul 20, 2022 at 12:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.