I typically do my error logging like this:

Sitecore.Diagnostics.Log.Error("My Error Message Here", ex, this);

But, that same syntax does not work for inside a Static class since this is not valid in a static property, static method, or static field initializer.

I see from the IntelliSense that my other options would be string loggerName or Type ownerType. But, I'm not sure exactly what these options mean.

What is the correct way to log an error inside a static class?

4 Answers 4


Actually, recommended practice would be to use a Namespace as your logger, as opposed to new object() or anything else. Just using "YourLoggerName" would not be flexible enough to allow admins to fine tune log handling inside your module.

As you rightly point out, this will not work in a static class. But this will:

Sitecore.Diagnostics.Log.Error("Message", ex, typeof(YourStaticClass));

Which is the exact equivalent of this.

  • 2
    I agree, this is the way. The type is what determines which log file to write to. Commented Mar 22, 2017 at 22:26
  • How about for a static function?
    – ADH
    Commented Mar 23, 2017 at 1:15
  • A static function would still live in a static class, and logging usually does not register on method level. this also refers to just the class name.
    – Mark Cassidy
    Commented Mar 23, 2017 at 9:34

You can also make use of a string for the object parameter as shown below:

Sitecore.Diagnostics.Log.Error("Message", ex, "YourLoggerName");

The loggerName is the module or feature. For example, you are using the Log.Error in the module Product, you can set the loggerName as Product. This can be useful when you see the errors with the logger as Product, which means it is being triggered in the Product Module.

The ownerType is a type. For example PropertyLayout, ExceptionLayout are different ownerType

What is the correct way to log an error inside a static class?

It all depends what you want to retrieve when you check the logs. Normally, you will make use of the Log.Error in your modules. So, it will be preferable to use the following:

Sitecore.Diagnostics.Log.Error("Message", ex, "YourLoggerName");

You can use an object (instance of a class), like below:

Sitecore.Diagnostics.Log.Error("Message", ex, new object());

this does not work, as the the Error() or any other logging method expects an instance of class, so it won't work in static method.

  • 1
    While this will work, better practice is use typeof(YourStaticClass) - if you ever define a dedicated log file for your own assemblies in future, it'll work as intended. Commented Mar 22, 2017 at 22:28

You could try to create a singleton rather than creating a static class. This should allow you to use "this."


  • 2
    I'd suggest to create a 'regular' class and register it as a Singleton using ether Sitecore DI (8.2+) or Sitecore Factory. Commented Mar 22, 2017 at 19:00

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.