Skip to content

Instantly share code, notes, and snippets.

@h09shais
Created August 17, 2018 16:31
Show Gist options
  • Save h09shais/f4d914131fbd1950259c94fcf996eb30 to your computer and use it in GitHub Desktop.
Save h09shais/f4d914131fbd1950259c94fcf996eb30 to your computer and use it in GitHub Desktop.
C# Write custom event log entry
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<!-- supported items-->
</startup>
<system.diagnostics>
<sources>
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="EventLog"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="ConsoleApp"/>
</sharedListeners>
</system.diagnostics>
</configuration>
try
{
if (!args.Any())
{
throw new InvalidOperationException("This is a sample invalid operation exception message");
}
}
catch (Exception exception)
{
using (var eventLog = new EventLog("Application"))
{
var stackTrace = new StackTrace(exception, true);
var frame = stackTrace.GetFrame(0);
var line = frame.GetFileLineNumber();
eventLog.Source = "Application";
eventLog.WriteEntry($"Message: {exception.Message}, Line: {line}", EventLogEntryType.Information, 333, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment