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

  1. Right-click app.config in Solution Explorer and choose Open.

    - or -

    If there is no app.config file:

    1. On the Project menu, choose Add New Item.

    2. From the Add New Item dialog box, choose Application Configuration File.

    3. Click Add.

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

  3. Add this element to that <listeners> section:

    <add name="FileLogListener" />
    
  4. Locate the <sharedListeners> section in the <system.diagnostics> section, nested under the top-level <configuration> section.

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

After you configure the file log listener for an assembly, it receives all messages that My.Application.Log writes from that assembly.

See also