VB My.Application.Log 範例

VB NET 內建提供了相當方便的LOG可以使用,適用於紀錄和程式監聽上面,
以下提供範例程式:


Config設定:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
  </startup>
  <system.diagnostics>
    <sources>
      <source name="DefaultSource" switchName="DefaultSwitch">
        <listeners>
          <add name="FileLogListener"/>
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
      <add name="FileLogListener"
           type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
           initializeData="FileLogListenerWriter"
           location="Custom"
           customlocation="C:\Logs\" />
    </sharedListeners>
  </system.diagnostics>
</configuration>

Module1.vb:

Sub Main()
        TracingTest("LogFile.txt")
        Console.ReadLine()
    End Sub

    Public Sub TracingTest(ByVal fileName As String)
        My.Application.Log.WriteEntry($"Entering TracingTest with argument '{fileName}'.")

        ' Code to trace goes here.

        My.Application.Log.WriteEntry($"Exiting TracingTest with argument '{fileName}'.")
    End Sub

留言

熱門文章