What is the best practice for logging in Umbraco?
Asked Answered
M

2

6

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)?

Mclane answered 28/9, 2015 at 12:56 Comment(0)
B
16

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.

Byrdie answered 28/9, 2015 at 14:2 Comment(3)
Great, thanks. LogHelper seemed as better choice to me on the first look, I just wanted to make sure I'm not missing something obvious - like a benefit of using one solution over the otherMclane
I'm using the log helper but I can't seem to get it to place any logs in the umbraco log file, is there anything I'm doing wrong? Do I have to configure anything at startup?Timmytimocracy
Generally not; perhaps you could describe how you're using it; and where you're looking for the entries? Also, what level are you logging at?Byrdie
M
5

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 :-)

Mccue answered 28/9, 2015 at 13:57 Comment(2)
Yeah, those were my thoughts too, but I was unsure if I'm missing something. ThanksMclane
No problem :-) Looks like there's a LoggingService on the way in 7.3.0, by the way.Mccue

© 2022 - 2024 — McMap. All rights reserved.