Logging in 2sxc / EAV
This should give you some minimal guidance into logging what your code does.
Use on Object
Object Creation
All objects which have a log, have a Log property which you can use to add notes.
This is the most common way to log.
Normally they will inherit from:
ToSic.Lib.Services.ServiceBase
This is for services which have a few dependencies.ToSic.Lib.Services.ServiceBase<Dependencies>
This is for services which have a lot of dependencies. In this case you'll have to pass the dependencies to the base class. Dependencies usually inherit fromToSic.Lib.Services.ServiceDependencies.ToSic.Lib.Services.HelperBaseThis is for helpers which are not services and will usually be created in code (not from DI).
You can also just create normal objects and implement ToSic.Sys.Logging.IHasLog.
Note that if you're inheriting from ServiceBase<Dependencies> then the dependencies class should
inherit from ToSic.Lib.Services.DependenciesBase.
Log Linking
For optimal log structures, they should be linked together. This helps to show the call hierarchy in the Insights.
This is fully automated, if you adhere to the conventions. It works as follows:
- Any object inheriting from
ServiceBase- should call
ConnectServicesin the constructor. TODO: EXAMPLE
- should call
- Any object inheriting from
ServiceBase<Dependencies>- should pass the dependencies into the base constructor.
- The dependencies are then available on a
Depsproperty. - For this to work, the
Dependenciesshould inherit fromServiceDependenciesand in the constructor callConnectServices.
Logging
Basic Messages
You can log messages with the following methods (they are kept very short to keep the code compact):
Log.A- add a messageLog.W- add a warningLog.E- add an error
All of these methods have a first string parameter containing the message to add.
They also all have an optional parameter called timer.
If this is set to true using timer: true then the log will also contain the time it took to execute the method.
Exceptions
You can log exceptions with the following methods:
Log.Ex- add an exception
Log Properties
Properties use getters and setters. To log these, you must consider a few aspects:
- do you want to log every single get/set? Or just the first one?
- do you want to log both get/set or just one of them?
To do this, you have 3 tools at your disposal:
Getter(() => result)- this will log the result of the getterSetter(() => result)- this will log the result of the setter- Create a
GetOnce<T>helper and on the get include theLog- this will only log the initial creation / get of the result
Log Methods, Functions, Properties
TODO:
History
- Introduced in 2sxc 9.6
- Added 2sxc Insights (server-side) v9.31
- Major enhancements in v10.22
- Moved to
ToSic.Sys.Loggingin v15.0