|
Microsoft Visual Basic. Net Programmer's Cookbook Authors: MacDonald M. Published year: 2003 Pages: 314-315/376 |
You need to write a log entry to the Windows event log on the current computer.
Register the event source and create the log if required. Write the message using the System.Diagnostics.EventLog class.
The EventLog class makes it easy to write and retrieve event log entries. You create an EventLog instance that wraps a specific log—typically the all-purpose Application log or a custom log that's used by your application.
' Access the Application log. Dim Log As New EventLog("Application")
You can also supply an additional argument to the EventLog constructor to specify a computer name . This allows you to access an event log defined on another computer on the network.
' Access the Application log. Dim Log As New EventLog("Application", "ComputerName")
To retrieve log entries, use the EventLog.Entries property, which provides a collection of EventLogEntry instances. To write a log entry, call the EventLog.WriteEntry method, specifying a text message and optionally other information, including an event log type (error, warning information, or security audit). However, before you attempt to write a message, you should make sure the current application is registered to write in the log, and you should call the EventLog.CreateEventSource method if it's not.
Here's an example Console application that writes a single log entry into a custom log and the displays all the entries in the log:
Public Module EventLogTest Public Sub Main() ' Register the event source if needed. If Not EventLog.SourceExists("MyApp1") Then ' This registers the event source and creates the custom log, ' if needed. EventLog.CreateEventSource("MyApp1", "MyNewLog") End If ' Create a log instance. Dim Log As New EventLog("MyNewLog") Log.Source = "MyApp1" ' Write a message. Log.WriteEntry("This is a test message.", _ EventLogEntryType.Information) ' Display all messages. Dim Entry As EventLogEntry For Each Entry In Log.Entries Console.WriteLine("Message: " & Entry.Message) Console.WriteLine("Written By: " & Entry.Source) Console.WriteLine("Written At: " & Entry.TimeWritten.ToString()) Console.WriteLine("Type: " & Entry.EntryType.ToString()) Console.WriteLine() Next Console.ReadLine() End Sub End Module
This code will work only it if executes under an account that has permission to modify the event log. To grant this permission to an account that lacks it, you must use regedt32.exe and find the HKEY_Local_Machine\SYSTEM\CurrentControlSet\Services\EventLog key. Then right-click the EventLog key, and select Permissions. Any account that is granted Full Control of this folder will be able to write to the event log.
You need to retrieve a list of the public queues in Active Directory or the private queues on a given computer.
Use the GetPublicQueues or GetPrivateQueuesByMachine methods of the System.Messaging.MessageQueue class.
.NET allows you to examine and administer message queues and the messages they contain using the classes in the System.Messaging namespace. You can create a MessageQueue class that wraps a specific message queue by specifying the queue path in the constructor.
' Check if a queue named MyQueue exists on the current computer. If MessageQueue.Exists(".\MyQueue") Then ' Show its
name
. Dim Queue As New MessageQueue(".\MyQueue") Console.WriteLine(Queue.QueueName) End If
In addition, you can retrieve all the queues in Active Directory using the shared MessageQueue.GetPublicQueues method, or you can retrieve the private queues on a given machine using the shared MessageQueue.GetPrivateQueuesByMachine method, in which case you must specify a computer name, or a period (.) to indicate the current computer.
The following code shows a simple Console application that enumerates all the queues on the current machine and displays the messages they contain. In order to use this code, you must add a reference to the System.Messaging.dll assembly and import the System.Messaging namespace.
Public Module GetQueues Public Sub Main() Dim Queue, Queues() As MessageQueue ' Get the private queues for the current machine. Queues = MessageQueue.GetPrivateQueuesByMachine(".") For Each Queue In Queues Console.WriteLine("Name: " & Queue.QueueName) Console.WriteLine("Path: " & Queue.Path) ' Get a snapshot of all messages (without removing them.) Console.WriteLine("Contains:") Dim Message, Messages() As Message Messages = Queue.GetAllMessages() For Each Message In Messages Console.WriteLine(Message.Id.ToString()) Console.WriteLine(Message.Body.ToString()) Next Console.WriteLine() Next Console.ReadLine() End Sub End Module
| Note |
You can add, remove, and administer message queues using the Computer Management console. Expand the Message Queuing node under the Services And Application node. You can right-click on a queue category folder to create a new queue for your application. |
|
Microsoft Visual Basic. Net Programmer's Cookbook Authors: MacDonald M. Published year: 2003 Pages: 314-315/376 |