Skip to end of metadata
Go to start of metadata

Writing log in your application might be a tedious work if not properly managed. One way to organize your logging code is by applying semantic logging design pattern. Semantic logging design pattern provides a centralized event source where you handle all your logging, and see how all the logs in your application are executed. Another benefit of using semantic logging design pattern is eliminating code redundancy.

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

On this page:

Overview

Using Crosslight Logging Framework, you can log your application using the ILog API such as ILog.Info, ILog.Debug, ILog.Error, and so on. This approach is manageable if you have simple logging in your application. As you develop more complex framework and modules, you might end up re-writing the same log in several places and each log might have complex handling. The following code shows a bad example on working with log in your application.

As you can see in the above code, the code readability and maintainability becomes compromised as logging code grow, not to mention some of these logging code are redundant. To address this issue, you can use semantic logging design pattern which is well supported in Crosslight Logging Framework.

Creating Event Source

To implement semantic logging design pattern, first you need to create a new event source that encapsulates all your logging code in your application. You can create several event sources for different modules or services within your application.

To create a new event source, you need to inherit from EventSource<T> or EventSource class. This event source later can be resolved using ILogService.GetEventSource method as follows.

ILogService.GetEventSource will create a singleton instance of the specified type when it first called by the application (lazy loading).
The following code shows how to centralize your logging code in a single event source, and call it across different classes in your application.

Using Event Source in Application

Once you have created an EventSource, you can resolve it using ILogService and start calling the method to log your application. The following code shows you how to use the event source in your application.

As you can compare from both examples above, it's obvious that the semantic logging pattern promotes a much cleaner and efficient way to perform logging on classes with similar logical functions. Semantic logging is also ideal for complex class scenario which contains many repetitive functions to be logged.

Samples

To learn more about application logging in Crosslight and see how it works, please checkout the following samples: