How to: Write Event Information to a Text File
You can use the My.Application.Log and My.Log objects to log information about events that occur in your application. This example shows how to use the My.Application.Log.WriteEntry method to log tracing information to a log file.
To add and configure the file log listener
Right-click app.config in Solution Explorer and choose Open.
- or -
If there is no app.config file:
On the Project menu, choose Add New Item.
From the Add New Item dialog box, choose Application Configuration File.
Click Add.
Locate the <listeners> section in the application configuration file.
You will find the <listeners> section in the <source> section with the name attribute "DefaultSource", which is nested under the <system.diagnostics> section, which is nested under the top-level <configuration> section.
Add this element to that <listeners> section:
<add name="FileLogListener" />
Locate the <sharedListeners> section in the <system.diagnostics> section, nested under the top-level <configuration> section.
Add this element to that <sharedListeners> section:
<add name="FileLogListener" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" initializeData="FileLogListenerWriter" location="Custom" customlocation="c:\temp\" />
Change the value of the customlocation attribute to the log directory.
Note
To set the value of a listener property, use an attribute that has the same name as the property, with all letters in the name lowercase. For example, the location and customlocation attributes set the values of the Location and CustomLocation properties.
To write event information to the file log
Use the My.Application.Log.WriteEntry or My.Application.Log.WriteException method to write information to the file log. For more information, see How to: Write Log Messages and How to: Log Exceptions in Visual Basic.
After you configure the file log listener for an assembly, it receives all messages that My.Application.Log writes from that assembly.
See Also
Tasks
How to: Log Exceptions in Visual Basic
Concepts
Working with Application Logs in Visual Basic