Sitecore 9 Dependency Injection – Extend the Sitecore Logging

With the extended use of Dependency Injection (DI) in Sitecore 9, you have yet another tool to extend the Sitecore functionality. With DI you can basically replace or extend standard Sitecore functionality with your own code.

Dependency Injection is not a replacement for Sitecore Pipelines, but a supplement.

Behold the following case: Replacing the Sitecore.Diagnostics.Log with your own implementation. In this case I will replace the Sitecore.Diagnostics.DefaultLog with my own implementation.

STEP 1: REGISTER THE NEW LOG CLASS

The Sitecore.Diagnostics.DefaultLog inherits from Sitecore.Abstractions.BaseLog, so my ServicesConfigurator needs to register a new class for that:

using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;

namespace MyCode
{
  public class ServicesConfigurator : IServicesConfigurator
  {
    public void Configure(IServiceCollection serviceCollection)
    {
      serviceCollection.AddTransient(typeof(Sitecore.Abstractions.BaseLog), typeof(MyCode.MyLog));
    }
  }
}

The ServicesConfigurator must be registered in Sitecore using a .config file. Make sure your config file is adding your section later than Sitecore. he who registers last, have the DI registration:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:env="http://www.sitecore.net/xmlconfig/env/">
  <sitecore>
    <services>
      <configurator type="MyCode.ServicesConfigurator, MyDll" />
    </services>
  </sitecore>
</configuration>

STEP 2: IMPLEMENT THE NEW LOG CLASS

That’s basically it.

Now I can replace Sitecore functionality by implementing a class that inherits from Sitecore.Abstractions.BaseLog:

using System;
using Sitecore.Caching;

namespace MyCode
{
  public class MyLog : Sitecore.Abstractions.BaseLog
  {
    public override void Audit(string message, string loggerName)
    {
      // Do something else
    }
	
	...
	...
	...
}

Or I can extend Sitecore functionality by inheriting from Sitecore.Diagnostics.DefaultLog:

using System;
using Sitecore.Diagnostics;

namespace MyCode
{
  public class MyLog: Sitecore.Diagnostics.DefaultLog
  {
    public override void Audit(string message, object owner)
    {
      // Adding the calling type to the message, thus extending the
	  // existing log messages
	  base.Audit(owner.GetType() + " " + message, owner);
    }

	...
	...
	...
  }
}

MORE TO READ:

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c#, General .NET, Sitecore 9 and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.