home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / logwrite.aspx < prev    next >
Encoding:
Text File  |  2000-06-08  |  3.4 KB  |  85 lines

  1.  
  2. <!-- #include virtual="/quickstart/howto/include/header.inc" -->
  3.  
  4. <h4>How Do I...Write to an event log?</h4>
  5.  
  6. <div class="indent" style="width:660"><font face="Tahoma" size="1">
  7.  
  8. <p>Event logging provides a standard, centralized way for 
  9. your applications to record important software and hardware events.
  10. Windows supplies a standard user interface for viewing the logs (Event Log
  11. Viewer). Using the NGWS runtime's EventLog component, you can easily connect to
  12. existing event logs on both local and remote computers and write entries to
  13. these logs. You can also read entries from existing logs and create your own
  14. custom event logs.</p>
  15.  
  16. <p>This sample illustrates how to write to an event log. It?s a
  17. small console application that can be run from a command prompt. The
  18. application takes three command line arguments. The first argument is the log
  19. to which the application will write. The second is the message to be written.
  20. The third is the name of the source of the event log entry (message).</p>
  21.  
  22. <p>Try running the sample as follows:</p>
  23.  
  24. </font><font face="Courier New" color="Blue" size="1"><blockquote>
  25.     > LogWrite.exe MyLog "Hello World!" "LogWrite Sample" 
  26. </blockquote></font><font face="Tahoma" size="1">
  27.  
  28. <p>Now, open the event log viewer and observe that there is a
  29. new log added to the list of logs on your machine. The log?s name is "MyLog"
  30. and it contains an entry written by "LogWrite Sample" (source). Double click on
  31. the entry. It will greet you with a "Hello World!" message.</p>
  32.  
  33. <p>In its simplest form, writing to an event log involves:</p>
  34.  
  35. <ol>
  36. <li>Registering an event source (if not already registered):
  37.  
  38.     </font><font face="Courier New" color="Blue" size="1"><p>
  39.         if(!EventLog.SourceExists(source)){<br>
  40.             EventLog.CreateEventSource(source,log);<br>
  41.         }
  42.     </p></font><font face="Tahoma" size="1">
  43.  
  44. <li>Instantiating an EventLog component and setting its Source property:
  45.  
  46.     </font><font face="Courier New" color="Blue" size="1"><p>
  47.         EventLog aLog = new EventLog();<br>
  48.         aLog.Source = source;
  49.     </p></font><font face="Tahoma" size="1">
  50.  
  51. <li>Calling the WriteEntry method:
  52.  
  53.     </font><font face="Courier New" color="Blue" size="1"><p>
  54.         aLog.WriteEntry(args[1],EventLogEntryType.Information);
  55.     </p></font><font face="Tahoma" size="1">
  56.  
  57. </ol>
  58.  
  59. <p>Now, you may want to try to modify the sample so it writes
  60. Error log entries instead of informational entries. ItÆs simple, just change
  61. the EventLogEntryType.Information to EventLogEntryType.Error, rebuild the
  62. sample and run it again!</p>
  63. </font></div>
  64.  
  65. <h4>Example</h4>
  66.  
  67. <p>
  68. <div class="indent">
  69. <a target="_blank" href="<%=Server.MapPath("/quickstart/howto/samples/Services/EventLog/LogWrite/LogWrite.exe")%>">
  70. <img style="border-color:black" border=1 src="/quickstart/images/genicon.gif"><br>
  71. </a>
  72. <div class="caption">LogWrite.exe</div><br>
  73. [<a target="_blank" href="<%=Server.MapPath("/quickstart/howto/samples/Services/EventLog/LogWrite")%>">View Sample</a>] | 
  74. [<a target="_blank" href="/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/Services/EventLog/LogWrite/LogWrite.src">View Source</a>]<p>
  75. </div>
  76.  
  77. <h4>Source Code</h4>
  78.  
  79. <div class="code">
  80. <xmp>
  81. <!-- #include virtual="/quickstart/howto/samples/Services/EventLog/LogWrite/LogWrite.cs" -->
  82. </xmp>
  83. </div>
  84.  
  85. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->