Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice for logging in Umbraco?

I noticed there are at least two ways to write logs in Umbraco. One way is using the LogHelper class, and the other is to use directly the methods of the instance obtained with log4net.LogManager.GetLogger method. Needless to say that LogHelper uses log4net itself, too.

I added a custom log appender, set the file destination of the appender to be the custom file (different than the default one) and tried both ways, and noticed that the results in log are more or less the same. LogHelper's entry looks like this:

ERROR ProjectName.Controllers.Backoffice.DataController - [P4876/T1/D2] Test

while the entry that log4net makes looks like this:

ERROR ProjectName.Controllers.Backoffice.DataController - Test

However, when I searched for posts about logging on Umbraco, I often found examples of using log4net directly, rather than using the LogHelper class provided by Umbraco CMS.

So basically, are there any good reasons developers should use one way instead of the other, or is it simply the matter of preference of using methods of the library instead of using CMS-provided helper class (or the other way around - helper before the library)?

like image 581
Misa Lazovic Avatar asked Sep 28 '15 12:09

Misa Lazovic


2 Answers

LogHelper is a convenience wrapper for logging - part of Logging framework with the Umbraco.Core.Logging.ILogger interface at it's core. By default the log4net implementation is used, but there's also an internal DebugDiagnosticsLogger class that outputs everything via Debug.WriteLine.

Generally, if you're developing using the Umbraco libraries, then LogHelper is the way to go.

like image 112
Robert Foster Avatar answered Nov 01 '22 06:11

Robert Foster


The LogHelper is probably created so that devs won't have to worry about which specific log provider is being used inside Umbraco (log4net, Elmah, whatever). So in theory it may add a tiny overhead, but it's easier to use.

I don't think there's an actual "best practice", but I'd use LogHelper :-)

like image 5
Jannik Anker Avatar answered Nov 01 '22 06:11

Jannik Anker