How to remove and create log in Windows Event Viewer


I have an app. I'm trying to write log in Windows Event Viewer when its crashing. I found <a href="https://stackoverflow.com/questions/25725151/write-to-windows-application-event-log" rel="nofollow">Write to Windows Application Event Log</a> and I'm using DispatcherUnhandledExceptionEventHandler for catching unhandled exception. I'm setting it in constructor of app like:

DispatcherUnhandledException += MyApplication_DispatcherUnhandledException;

and write log like this:

using (EventLog eventLog = new EventLog("Application")) { eventLog.Source = "Application"; eventLog.WriteEntry(exceptionMessage, EventLogEntryType.Error); }

Log creates, but in Run method of System.Windows.Application occurs another exception and windows adds this error in Event Viewer with another Id, source....

Description: The process was terminated due to an unhandled exception. <blockquote>

Exception Info: System.Exception at ServerApp.MainWindow..ctor()

Exception Info: System.Windows.Markup.XamlParseException at System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri) at System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows.Markup.ParserContext, System.Object, Boolean) at System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream, System.Windows.Markup.ParserContext) at System.Windows.Application.LoadComponent(System.Uri, Boolean) at System.Windows.Application.DoStartup()


How can I write only my log in event viewer?


using System; using System.Diagnostics; ... ... public void WriteToEventLog(EventLogEntryType eventLogType, string message, string logSourceName) { if (!EventLog.SourceExists(logSourceName)) { EventLog.CreateEventSource(logSourceName, "Application"); } using (var eventLog = new EventLog { Source = logSourceName }) { const int maxLength = 31000; if (message.Length > maxLength) { message = message.Substring(0, maxLength); } eventLog.WriteEntry(message, eventLogType); } }

The user, under which account this app is going to run, needs to have access to be able to create logs.

Good luck.


I would put a try catch around the whole process and then write the exception to a file see:

<a href="https://stackoverflow.com/questions/21307789/how-to-save-exception-in-txt-file" rel="nofollow">how to save exception in txt file?</a>

You don't have to always write to the file but just to understand what is really going on


  • GMT Offset with inputs: latitude and longitude
  • How to place UI widgets on top of multiple Z ordered Surface Views in Android
  • how to change button text after succes in ajax
  • GAE python NDB projection query working in development but not in production
  • Tortoise SVN conflict occurs but no edit conflict button is shown
  • Identify File Type in Java
  • Alternative to bcrypt when saving passwords in PHP 5.2
  • QNetworkAccessManager one instance and connecting slots
  • Using MPI_Send/Recv to handle chunk of multi-dim array in Fortran 90
  • Python tk scrollbar becomes inactive once text is outside the screen
  • Tensorflow converging but bad predictions
  • Get all categories and items in category
  • How to get WinForms custom control's default value to be respected when first dropped on a form
  • Selenium Webdriver IE could not find element
  • SpringBoot don't replacen System variable {user.home} in Spring Tool Suite Version: 3.8.4.RELEA
  • Bison does not appear to recognize C string literals appropriately
  • Getting Microsoft Calibri font on Amazon EC2 ubuntu
  • Admob in ListView not clickable
  • JavaMail connection problems [duplicate]
  • How to use array in autohotkey?
  • How to make Rss News Reader application in android …? [closed]
  • Getting the type of a “Type” in C# reflection
  • Disable account chooser FirebaseUI React
  • SQL Server 2012 not showing unicode character in results
  • Java .policy file - how to prevent java.util.Date() from being accessible
  • Add font awesome icon to custom add to cart button in Woocommerce 3
  • how to read to huge file into buffer
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • Calculate time from document
  • 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • Change cell value based on cell color in google spreadsheet
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • Using redis as an LRU cache for postgres
  • Google App Engine Datastore: Dealing with eventual consistency
  • How to handle div that is created dynamically in a table
  • Functions by reference or by variable, which to use when?
  • Google App Engine backend servlet not responding
  • ssh remote server login script
  • How to call different template for different category archive page in woocommerce