5

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?

10

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.

| improve this answer | |
  • 2
    I agree, this is the way. The type is what determines which log file to write to. – Steve McGill Mar 22 '17 at 22:26
  • How about for a static function? – ADH Mar 23 '17 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 Mar 23 '17 at 9:34
1

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");
| improve this answer | |
0

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.

| improve this answer | |
  • 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. – Steve McGill Mar 22 '17 at 22:28
0

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

https://msdn.microsoft.com/en-us/library/ff650316.aspx

| improve this answer | |
  • 2
    I'd suggest to create a 'regular' class and register it as a Singleton using ether Sitecore DI (8.2+) or Sitecore Factory. – Serhii Shushliapin Mar 22 '17 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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