Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You call CreateEventSource if the source does not already exist and then use WriteEntry to write to the log. There are a couple of things to keep in mind, though.</p> <p>CreateEventSource will need admin access the first time the program is run. I always just keep a short command-line program around that takes the Event Source as an argument. Run it as admin during installation just once, and then your program can write events without problem under an appropriate level of access.</p> <p>WriteEntry also takes an entry type and an error number, like:</p> <pre><code>myEventLog.WriteEntry("Health Check. Program started normally",EventLogEntryType.Info,1011); </code></pre> <p>or</p> <pre><code>myEventLog.WriteEntry("Error. Could not open database connection!",EventLogEntryType.Error,1012); </code></pre> <p>These can be helpful, since monitoring systems like Microsoft Operations Manager can be set to watch these and notify you or the persons on call. I usually create a unique set of error numbers so that the sysadmins know who to call; me, a dba, a vendor's help line to report their web service down, and so on. Saves you a lot of 2AM calls.</p> <p>Here's a sample: </p> <pre><code>using System.Diagnostics; class TestClass { private EventLog log; public TestClass() { if(!EventLog.SourceExists("TestApplication")) { EventLog.CreateEventSource("TestApplication","Application"); } log=new EventLog("Application",".","TestApplication"); } public int ParseFile(StreamReader sr) { string[] lines=sr.ReadToEnd().Trim().Split('\n'); int linecount=lines.Length; string connString=System.Configuration.ConfigurationSettings.AppSettings["ConnectString"]; SqlConnection conn=new SqlConnection(connString); try { conn.Open(); } catch (Exception e) { log.WriteEntry("Cannot connect to database for file import: "+e.Message, EventLogEntryType.Error,1171); return linecount; } // write to database, etc. } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload