How to: Write Event Information to a Text File (Visual Basic)
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.
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=220.127.116.11, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" initializeData="FileLogListenerWriter" location="Custom" customlocation="c:\temp\" />
Change the value of the customlocation attribute to the log directory.
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 (Visual Basic) 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.