Created
August 17, 2018 16:31
-
-
Save h09shais/f4d914131fbd1950259c94fcf996eb30 to your computer and use it in GitHub Desktop.
C# Write custom event log entry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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