Log sources and trace listeners define set of rules and action for the log entries that have passed the filtering process. Defining a log source means defining which log entry should be processed by the log source and defining which trace listeners to be used to process the log entry.
In general, you create several log sources to categorize how you want to handle the log entries. You might want to use the default log source to streamline all your log to file repository, but you might want to have all log with Fatal severity sent directly to your administrator mail. To do this, you need to create and register a new log source that will process all the critical logs.
Configuring Default Log Source
A log source contains a set of rules that define how the log entry should be processed. Specifically, the rules in log source consisted of two main components which is filters and trace listeners. The log writer may contain multiple log sources, and by default, it has one LogSource that accepts all log entries. The default log source can be accessed through the log writer's AllEventsTraceSource property where you can configure the log source settings, add log filters, and add trace listeners to it.
The following code example shows how to remove all existing trace listeners from the default log source, then add a new ListTraceListener instance to the default log source.
Adding New Log Source
In rare cases, you might want to process the log filters and the listeners differently from the default log source. Crosslight Logging Framework supports multiple log sources where each log will be processed sequentially. To create a new log source, you need to define the filter rules of your log source. Afterward, you specify one or more trace listeners to handle your log entries.
The following code example shows how to add a new log source to the log writer.
Understanding Trace Listeners
From the above examples, there are several trace listeners that can be configured in the log source. These trace listeners encapsulate the logic to handle the log entry differently. Crosslight Logging Framework provides several built-in trace listeners as follows.
Writes the log entry to output window.
Writes the log entry to specific file configuration.
Writes the log entry to in-memory array list.
- AzureInsightTraceListener (available in Intersoft.Crosslight.Logging.AzureInsights)
Send the log entry to Azure Application Insight service.
- EntityFrameworkTraceListener (available in Intersoft.Crosslight.Logging.EntityFramework.Server)
Write the log entry to database through entity framework code first.
Creating Custom Trace Listener
You can also create your own custom trace listener by inheriting the TraceListener class. The following code shows important aspect on how to handle log entry in a trace listener.
As you can see, there are two entry points that you can choose to handle your log entry. If you want to access the log entry object, you can handle the log entry in TraceData method. Alternatively you can use the formatted log entry string in Write and WriteLine method. To learn more about the log formatter see Using Log Formatter.
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