Skip to end of metadata
Go to start of metadata

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. 

To learn more about the Crosslight logging concept in general, see Implementing Application Logging.

On this page:

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.

Log output

Resolving ILog from ILogService

If you want to log from classes that aren't derived from ViewModelBase, you can manually resolve the ILog object from ILogService as follows.

Using Tracer to Log Code Performance

In addition to writing log info using Logger.Info, Logger.Debug, etc. you can also use the specialized API in Crosslight Logging Framework to log code performance as follows.

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: