Write to the Event Log in .NET (VB.NET and C#.NET)

It is as simple as this to write to the Windows Event Log in .NET

Declaration

Imports System.Diagnostics

Code

Public Function WriteToEventLog(ByVal Entry As String, _
  Optional ByVal AppName As String = "APlusFeeCalc", _
  Optional ByVal EventType As EventLogEntryType = EventLogEntryType.Information, _
  Optional ByVal LogName As String = "Application") As Boolean
     Dim objEventLog As New EventLog
     Try
          'Register the App as an Event Source
          If Not objEventLog.SourceExists(AppName) Then
               objEventLog.CreateEventSource(AppName, LogName)
          End If
          objEventLog.Source = AppName
          'WriteEntry is overloaded; this is one of 10 ways to call it
         objEventLog.WriteEntry(Entry, EventType)
          Return True
     Catch Ex As Exception
          Return False
     End Try
End Function
Chris Pietschmann
Chris Pietschmann
Microsoft MVP | App Innovation Leader | Azure, AI & DevOps Architect | HashiCorp Ambassador | Author
I'm a Practice Leader, App Innovation specialist, solution architect, developer, SRE, trainer, and author with 25+ years of experience helping enterprises turn AI, app modernization, cloud architecture, and DevOps into real business outcomes.