4

The solution is having two projects Web and Service Layer. Service Layer do not have any dependency with Sitecore. However the exceptions needs to be logged into Sitecore log files in data folder. What is the best way to do this?

2 Answers 2

5

Sitecore logging system is wrapper under log4net. It is even configured not under <sitecore> node in web.config.

To use Sitecore log files you should have 3 things:

  1. Configured <log4net> section in web.config file. (If your service layer is hosted from same folder as Sitecore then nothing should be changed. Otherwise you can copy log4net section to your web config file)
  2. You need reference Sitecore.Kernel assembly to your Service Layer application. Sitecore log4net logging wrapper is inside it.
  3. You could use Sitecore.Diagnostics.Log.Log()(Debug/Warn/Error/Audit) to write logs.

If you want independent logs for you service layer then configure separately log4net.

2
  • 1
    Didn't the OP ask for a solution that didn't introduce a Sitecore dependency to the Service Layer project? Nov 21, 2017 at 16:16
  • @MatthewFitzGerald-Chamberlain as far as I understand OP he wrote that "Service Layer do not have any dependency with Sitecore.", but he didn't asked solution without dependency. If he didn't want dependency on Sitecore then similar could be done on log4net without Sitecore dependency. I mentioned it in last sentence.
    – Anton
    Nov 21, 2017 at 19:49
1

If your Service Layerdoesn't has any dependencies with Sitecore and it can't be added (in some reasons), I can suggest adding an additional class libriary (Project), then adding the reference to the Sitecore.Kernel

Secondly, you need add something like a wrapper for the Sitecore Loger:

 using System;

namespace Your.Namespace
{
public static class LogService
{

    public static void Debug(string message){

        Sitecore.Diagnostics.Log.Debug(message);
    }

    public static void Debug(string message, Exception exception)
    {
        Sitecore.Diagnostics.Log.Debug(message, exception);
    }

    public static void Info(string message, object owner)
    {
        Sitecore.Diagnostics.Log.Info(message, owner);
    }

    public static void Info(string message, Exception exception)
    {
        Sitecore.Diagnostics.Log.Info(message, exception);
    }

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

    public static void Warn(string message, Exception exception)
    {
        Sitecore.Diagnostics.Log.Warn(message, exception);
    }

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

    public static void Error(string message, Exception exception)
    {
        Sitecore.Diagnostics.Log.Error(message, exception);
    }

    public static void Fatal(string message, object owner)
    {
        Sitecore.Diagnostics.Log.Fatal(message, owner);
    }

    public static void Fatal(string message, Exception exception)
    {
        Sitecore.Diagnostics.Log.Fatal(message, exception);
    }

    }
}

Now you can add to the Service Layer a reference to newly added project and use LogService for writing to the Sitecore Log.

Your Answer

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

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