Logging events to a disk file
This scenario is one of several that describe the typical requirements for using the Semantic Logging Application Block in your applications. It describes the process for setting up the block to send log events to a disk file.
Typical Goals
You need to send logging information created as log entries to one or more sinks or targets, and the list of targets includes a disk file. You want to set up the Semantic Logging Application Block to use a suitable event sink, decide what type of file to create (text, JSON, or XML), and configure the format and other file logging options you require.
Notes
The major difference between the two types for event sink you can use for storing events in flat files is that the Rolling Flat File event sink allows you to start a new log file at specified intervals.
You should not configure more than one Flat File sink or Rolling Flat File sink instance to write to the same physical file. However, you can enable the same listener (and sink) on multiple event sources and funnel the events from those multiple event sources to the same file.
If you use the XmlEventTextFormatter class to write the log entries as XML, the formatter class writes the log entries to comply with the schema described in the Event Schema topic on MSDN. Tools such as PerfView can load XML files that use this schema.
Solution
The following steps describe the process, and provide links to more details for each step.
To configure logging to a disk file
- Define an event source for your application. For more information, see the topic Developing event sources using the .NET EventSource class.
- Write code to raise the events you defined in response to actions within the application. For more information, see the topic Raising events from your applications.
- Decide if you will use the in-process or out-of-process approach for collecting events. For more information to help you decide, see the topic Overview of logging using the Semantic Logging Application Block.
- If you choose to use the in-process approach:
- Use NuGet to add the Semantic Logging Application Block and the Semantic Logging Application Block – Text file sinks packages to your Visual Studio project. For more information, see the topic Using the block in applications for the in-process scenario.
- Add code to configure either the Flat File or the Rolling Flat File event sink. You can specify a formatter for the event sink to output the information in log entries as JSON, XML or as simple text. For more information, see the topics Choosing and using event sinks and Choosing and using text formatters.
- If you choose to use the out-of-process approach:
- Install the Out-of-Process Host service application. For more information, see the topic Installing and running the Out-of-Process Windows Service/Console Host.
- Configure the Out-of-Process Host to use either the Flat File or the Rolling Flat File event sink. You can specify a formatter for the event sink to output the information in log entries as JSON, XML or as simple text. For more information, see the topics Using the trace event service for the out-of-process scenario, Choosing and using event sinks, and Choosing and using text formatters