How Orchard CMS does the logging?
Asked Answered
J

2

9

I'm working with Orchard CMS and it is better CMS for me. I want to understand how it does the logging and whether I can add my own logging or not. I saw that Orchard uses NullLogger class and it does no work. I've opened the App_Data.Logs folder and have seen that there are the log files. But how? I searched in code where is the trick that replaces NullLogger with log4net (I guess this is log4net, because the log format and the formatting for log4net.config are very similar) but I haven't found this. Can somebody answer me:

  1. How Orchard does the logging?
  2. Whether I can add my own logger and if yes what best practices exist to do this?

Thanks, Andrey.

Jethro answered 10/2, 2012 at 8:47 Comment(0)
H
16

An Autofac module (Orchard.Logging.LoggerModule to be precise) handles that. Basically - it scans each dependency and fills all properties of type ILogger with a reference to appropriate logger instance. Each dependency gets its own logger with name equal to full type name (including namespace) of a containing class.

The NullLogger is just a placeholder so accessing the property would not throw NullReferenceExceptions before the property is being set by Autofac.

Extending the default logging is a rather complicated task as it would involve doing three things:

  • create a custom implementation of ILoggerFactory (just like the default Orchard.Logging.CastleLoggerFactory) and
  • create an Autofac module that registers that implementation in the container (like the mentioned LoggerModule does)
  • suppress the current default logging module by decorating your new one with [OrchardSuppressDependency("Orchard.Logging.LoggingModule")]

UPDATE

Just realized I haven't addressed the most important part of the question here:) Yes, Orchard uses log4net so you may alter the default settings via Config/log4net.config file.

Honig answered 10/2, 2012 at 10:11 Comment(4)
Thanks a lot, it is useful for me. But I don't understand yet in which place of the Orchard a currently used logger is configured. It is in log4net.config file or somewhere else?Jethro
Ok, I've found the answer. It really uses log4net.config files. Thanks.Jethro
Is there a way I could create a logger to handle all logging for a particular namespace? Or is it specific to the class?Renaldo
It's specific to a class, but you may try to tinker with the LoggerModule, where all the magic of per-class instantiation happens.Honig
R
1

There is a great article on how Orchard Logging works. (I am not sure if it is ok to copy and paste the entire article). This is the link: Injection Logger in Orchard

So the trick is this:

Whenever a class requires a Logger instance, all it needs to do is to declare a ILogger property, that’s it. And later, in your class, you can use this property to Logging at anytime

And how is this done?

When Orchard web application startup, the OrchardStarter will be used to do most of the registration work.

In a few words, it looks all the code in all projects, gets all the classes that use an ILogger property, and implements it for you (if not implemented), using Castle's logger factory.

Refuse answered 21/3, 2016 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.