(NET) NET (2022)

EventLogger - example of Task.Factory.FromAsync, Task.Run, ReaderWriterLock, Interlocked.Increment, Stream.BeginWrite, SyncLock, Timeout, AsyncCallback, IAsyncResult, String.Format, Timespan.

This is extremely short and simple program, but very useful for beginners to lean Visual Basic. This program only write events to text file from multi threading application, but doing this simple job need experience on VB.NET, therefore I decide to show my code for beginners.

This log also can be deleted in any time.



This is a schema to perform this log.



In the output window you can see a message log for increase timeout.


And this is my code.



   1:  Imports System.ComponentModel.DataAnnotations
   2:  Imports System.Globalization
   3:  Imports System.IO
   4:  Imports System.Text
   5:  Imports System.Threading
   6:  Imports System.Threading.Tasks
   7:  Imports Telerik.WinControls.UI.Barcode.Symbology
   8:   
   9:  Public Module TxtLogger
  10:      Dim RWL As ReaderWriterLock = New ReaderWriterLock()
  11:      Dim Timeout As TimeSpan = TimeSpan.FromMilliseconds(100)
  12:      Dim TimeoutLock As New Object
  13:      Dim Counter As Integer
  14:      Const MsgLength As Integer = 70
  15:      Public Sub WriteLog(Msg As String, Str1 As StringBuilder)
  16:          Interlocked.Increment(Counter)
  17:  Again:
  18:          Try
  19:              RWL.AcquireWriterLock(Timeout)
  20:              Try
  21:                  Str1.AppendLine($"{Counter},{RWL.WriterSeqNum},{Now.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)},{Msg.Substring(0, Math.Min(MsgLength - 20, Msg.Length))}".PadRight(MsgLength - 2) & vbCrLf)
  22:                  Using Stream = File.Open("Time.csv", FileMode.OpenOrCreate, FileAccess.Write)
  23:                      Dim T As Task = Task.Factory.FromAsync(Function(Callback As AsyncCallback, State As Object)
  24:                                                                 Stream.Position = RWL.WriterSeqNum * MsgLength
  25:                                                                 Return Stream.BeginWrite(Text.UTF8Encoding.UTF8.GetBytes(Str1.ToString), 0, MsgLength, Callback, State)
  26:                                                             End Function,
  27:                                                 Sub(X As IAsyncResult)
  28:                                                     Stream.EndWrite(X)
  29:                                                 End Sub,
  30:                                                 Nothing)
  31:                      T.Wait()
  32:                      Stream.Close()
  33:                  End Using
  34:              Finally
  35:                  RWL.ReleaseWriterLock()
  36:                  Str1 = Nothing
  37:              End Try
  38:          Catch ex As Exception
  39:              Console.WriteLine($"{Counter},{RWL.WriterSeqNum},{Now.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)},{Msg},{ex.Message}" & vbCrLf)
  40:              SyncLock TimeoutLock
  41:                  Timeout = Timeout.Add(TimeSpan.FromMilliseconds(100))
  42:              End SyncLock
  43:              GoTo Again
  44:          End Try
  45:      End Sub
  46:   
  47:  End Module

Update, write to many files and with file suffix.





Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: http://www.vb-net.com/EventLogger/Index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>