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.
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.
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.
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