home *** CD-ROM | disk | FTP | other *** search
-
- <!-- #include virtual="/quickstart/howto/include/header.inc" -->
-
- <h4>How Do I...Write to an event log?</h4>
-
- <div class="indent" style="width:660"><font face="Tahoma" size="1">
-
- <p>Event logging provides a standard, centralized way for
- your applications to record important software and hardware events.
- Windows supplies a standard user interface for viewing the logs (Event Log
- Viewer). Using the NGWS runtime's EventLog component, you can easily connect to
- existing event logs on both local and remote computers and write entries to
- these logs. You can also read entries from existing logs and create your own
- custom event logs.</p>
-
- <p>This sample illustrates how to write to an event log. It?s a
- small console application that can be run from a command prompt. The
- application takes three command line arguments. The first argument is the log
- to which the application will write. The second is the message to be written.
- The third is the name of the source of the event log entry (message).</p>
-
- <p>Try running the sample as follows:</p>
-
- </font><font face="Courier New" color="Blue" size="1"><blockquote>
- > LogWrite.exe MyLog "Hello World!" "LogWrite Sample"
- </blockquote></font><font face="Tahoma" size="1">
-
- <p>Now, open the event log viewer and observe that there is a
- new log added to the list of logs on your machine. The log?s name is "MyLog"
- and it contains an entry written by "LogWrite Sample" (source). Double click on
- the entry. It will greet you with a "Hello World!" message.</p>
-
- <p>In its simplest form, writing to an event log involves:</p>
-
- <ol>
- <li>Registering an event source (if not already registered):
-
- </font><font face="Courier New" color="Blue" size="1"><p>
- if(!EventLog.SourceExists(source)){<br>
- EventLog.CreateEventSource(source,log);<br>
- }
- </p></font><font face="Tahoma" size="1">
-
- <li>Instantiating an EventLog component and setting its Source property:
-
- </font><font face="Courier New" color="Blue" size="1"><p>
- EventLog aLog = new EventLog();<br>
- aLog.Source = source;
- </p></font><font face="Tahoma" size="1">
-
- <li>Calling the WriteEntry method:
-
- </font><font face="Courier New" color="Blue" size="1"><p>
- aLog.WriteEntry(args[1],EventLogEntryType.Information);
- </p></font><font face="Tahoma" size="1">
-
- </ol>
-
- <p>Now, you may want to try to modify the sample so it writes
- Error log entries instead of informational entries. ItÆs simple, just change
- the EventLogEntryType.Information to EventLogEntryType.Error, rebuild the
- sample and run it again!</p>
- </font></div>
-
- <h4>Example</h4>
-
- <p>
- <div class="indent">
- <a target="_blank" href="<%=Server.MapPath("/quickstart/howto/samples/Services/EventLog/LogWrite/LogWrite.exe")%>">
- <img style="border-color:black" border=1 src="/quickstart/images/genicon.gif"><br>
- </a>
- <div class="caption">LogWrite.exe</div><br>
- [<a target="_blank" href="<%=Server.MapPath("/quickstart/howto/samples/Services/EventLog/LogWrite")%>">View Sample</a>] |
- [<a target="_blank" href="/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/Services/EventLog/LogWrite/LogWrite.src">View Source</a>]<p>
- </div>
-
- <h4>Source Code</h4>
-
- <div class="code">
- <xmp>
- <!-- #include virtual="/quickstart/howto/samples/Services/EventLog/LogWrite/LogWrite.cs" -->
- </xmp>
- </div>
-
- <!-- #include virtual="/quickstart/howto/include/footer.inc" -->