Writing log entries using Crosslight Logging Framework is very straightforward. You simply obtain access to ILog object that is already available in ViewModelBase through the Logger property, or resolve the ILog object using ILogService provided by Crosslight Logging Framework.
With the ILog object, you can start writing your log using the provided API.
Using Logger in ViewModelBase
If your ViewModel class is derived from ViewModelBase, you can use the Logger property to write your log. Through this property, you can start writing log by using the APIs such as shown in the following code example.
Using Tracer to Log Code Performance
As seen in the above example, the Tracer log is automatically started and stopped when using it with the C# using statement which works perfectly in async methods. In addition, the Tracer also automatically capture the stack trace of the caller method, as well as the elapsed time during the trace session.
Understanding Log Entry Category
Each log entry has a category. This category is auto-filled by the ILog object. By default the category is set to the full class name where the ILog object is referenced. So if you perform logging in ApplicationLoggingSamples.ViewModels.MyModule class, the category will be ApplicationLoggingSamples.ViewModels.MyModule, the full name of the class. This complies to the per-class logging concept, where all log entry category should indicates where the log is created.
Per-class logging concept gives you a more straightforward way to filter out your log entry. You can also filter out the log entry based on partial namespace or exact class name. To learn more how to filter log entries, see Filtering Log Entries.
If you need to change the default category passed by the ILog object, you can do that by overriding the Logger property, or resolving it with specific category as shown in the following code.
Understanding Log Entry Priority and EventId
Beside category, severity, message, timestamp, LogEntry class also has other properties that you can use such as Priority and EventId. These two properties is not assignable from ILog object. However you can set this property through extra data as follows.
To learn more about application logging in Crosslight and see how it works, please checkout the following samples:
- Application Logging samples, demonstrates the logging best practice in Crosslight application – http://git.intersoftpt.com/projects/CROS-SUPP/repos/application-logging-sample/browse
- Logging samples, demonstrates each logging feature and built-in trace listeners available in Crosslight Logging Framework – http://git.intersoftpt.com/projects/CROS-SUPP/repos/logging-samples/browse