0

Using Sitecore 10.2

I'm trying to log only my application logs into the database. The following approach works, but for the Columns Logger & Message, I would like to change the values.

Sitecore.config

<root>
        <level value="DEBUG" />
        <appender-ref ref="DbLogAppender"/>
      </root>
      <appender name="DbLogAppender" type="Stratum.Foundation.Logging.Models.DbLogAppender, Stratum.Foundation.Logging">
        <bufferSize value="1" />
        <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <connectionStringName value="logs"/>
        <param name="CommandText" value="usp_InsertLogFromApp" />
        <param name="CommandType" value="StoredProcedure" />            
        
        <parameter>
          <parameterName value="@logger" />
          <dbType value="String" />
          <size value="255" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%c" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@message" />
          <dbType value="String" />
          <size value="4000" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%m" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@exception" />
          <dbType value="String" />
          <size value="4000" />
          <layout type="log4net.Layout.ExceptionLayout" />          
        </parameter>
      </appender>

DbLogAppender.cs

public class DbLogAppender : ADONetAppender
{
   protected override void Append(LoggingEvent theEvent)
        {
            var eventData = theEvent.GetLoggingEventData();
            
            if (eventData.Message.Equals("My_Log", StringComparison.InvariantCultureIgnoreCase))
            {
                base.Append(theEvent);
            }
        }
}

This is how I'm logging the exception.

BannersController.cs

namespace Stratum.Feature.PageContent.Controllers
{
   public class BannersController : Controller
    {
       public ActionResult RenderHeroBanner()
        {
            try
            {
                Convert.ToInt32("asd");
            }
            catch (Exception ex)
            {
                Log.Error("My_Log", ex, this);
            }
        }
    }
}

In the database, the record is created like this:

Logger Message Exception
Stratum.Feature.PageContent.Controllers.BannersController My_Log Exception: System.FormatException Message: Input string was not in a correct format. .......

In the Logger column, I would like to have the the method name also.
Current: Stratum.Feature.PageContent.Controllers.BannersController
Expected: Stratum.Feature.PageContent.Controllers.BannersController.RenderHeroBanner

In the Message column, I would like to have the "Message" of the "Exception".
Current: My_Log
Expected: Input string was not in a correct format

In Sitecore.config, I have tried multiple conversionPatterns to achieve these, like %method, %stracktrace, %stracktracedetails, %location. But they return empty values.

For what I'm expecting, is it possible OOB or do I need to extend any code. If so, please let me know the way.

1 Answer 1

1

You could try a decorator w/ ILog Dependency and create a method something like and use that method in your client code

 public void Error(string message, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int lineNumber = 0)
 {
     _Log.Error($"[{Path.GetFileNameWithoutExtension(sourceFilePath)}.{memberName}:{lineNumber}] - {message}");
 }
2
  • Sorry this was unclear. Could you please provide more explanation like where this new method should be created. Is it in my DbLogAppender.cs. If yes, then where should it be called.
    – sukesh
    Mar 21, 2023 at 3:01
  • Also, I had 2 questions. Does this answer include solution for both.
    – sukesh
    Mar 21, 2023 at 3:07

Your Answer

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

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