AspNet.sk     Diskusné fóra     Vitajte v QuickStarts     ASP.NET     Silverlight     Ako môžem...? (en)     Class prehliadač Príklady chcem v ...   
Menu
Skip Navigation Links.

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Write to an event log?



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 (the Event Viewer). Using the common language runtime's EventLog component, you can easily connect to existing event logs on a local computer, and write entries to these logs. You can also read entries from existing logs and create your own custom event logs.

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).

Try running the sample as follows:
> LogWrite.exe MyLog "Hello World!" "LogWrite Sample"


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.

In its simplest form, writing to an event log involves:
  1. Registering an event source (if not already registered):

    
    If (Not EventLog.SourceExists(source)) Then
        EventLog.CreateEventSource(source, log)
    End If
    
    VB


  2. Creating a new instance of an EventLog component and setting its Source property:

    
    Dim aLog As New EventLog
    aLog.Source = source
    
    VB


  3. Calling the WriteEntry method:

    
    Dim message As String = "some message"
    aLog.WriteEntry(message, EventLogEntryType.Information)
    
    VB




Now, you may want to try to modify the sample so it writes error log entries instead of informational entries. Just change the EventLogEntryType.Information to EventLogEntryType.Error, rebuild the sample and run it again.

Example

VB LogWrite.exe
View Source

[This sample can be found at H:\Home\WU_000036_efe47225c86ca62f325a01d8519bc002\Webs\aspnet.sk\quickstarts\QuickStartv20\howto\samples\Services\EventLog\LogWrite\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin directory.]




Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.


Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright © 2005 Microsoft Corporation. All rights reserved.
Preklad do slovenského jazyka - Copyright © 2005 - 2007 www.aspnet.sk, www.qsh.sk
Pošlite komentár k tejto stránke
Copyright © 2002 - 2008 Chastia, spol. s r. o., Igor Stanek, Designed by Lacino
Portál je hostovaný na serveroch firmy Quantasoft - www.qsh.sk.