.NET Tip: Writing to the Event Log Irvine CA

Create a class to add Error, Warning, and Information entries in the Windows event log.

Local Companies

Applied Knowledge, Inc.
714) 751-3480
1502 Brookhollow, Suite A
Santa Ana, CA
Integrated Sales Management, Inc
(877) 553-0485
7545 Irvine Center Dr. Suite 200
Irvine, CA
Network Synthesis
949-453-1805
355 Goddard, Suite 200
Irvine, CA
Strategix Performance, Inc.
(801) 363-7267
1280 Bison West
Newport Beach, CA
PreScreened Consultants
1-888-242-0777
660 Baker Street Suite 277
Costa Mesa, CA
BHNET Software Solutions
949-273-4095
26861 Trabuco Rd
Mission Viejo, CA
ADM Systems
(323) 646-1317
17375 Brookhurst Street
Fountain Valley, CA
ThoughtFocus
866-831-4284
21161 Foxtail
Mission Viejo, CA
JNC Solutions, Inc
714.590.9017
12192 Brookhurst St. Suite 375
Garden Grove, CA
ACG Technologies
949.713.5162 x109
22521 Avenida Empresa
Rancho Santa Margarita, CA

.NET Tip: Writing to the Event Log

provided by: 
Originally published at Internet.com


The Windows The Windows event log is great place to log pertinent information from your application. There is built-in support for accessing the event log in .NET that you can easily implement. Here, I would like you show you a simple method to add entries to the Application event log. First, take a look at the code to use a new Logger class that writes to the event log. Logger Log = new Logger("CSTips"); Log.AddError("Error message"); Log.AddInformation("Informational message"); Log.AddWarning("Warning message");

The first line of code creates a new instance of the Logger class. The constructor of the Logger class requires a parameter that indicates the source of the messages. This would typically be the name of your application. The following three lines of code write error, information, and warning entries respectively to the event log. Using the Logger class is very straightforward and keeps your application code clean. Now, take a look at the Logger class below to see how the entries are added to the event log. public class Logger { private EventLog _EventLog; public string _Source { get; set; } public string _Log { get; set; } public Logger(string SourceName) { _EventLog = new EventLog(); _Source = SourceName; if (!EventLog.SourceExists(_Source)) { EventLog.CreateEventSource(_Source, _Log); } _EventLog.Source = _Source; } public void Add(string Message, System.Diagnostics.EventLogEntryType eType) { _EventLog.WriteEntry(Message, eType); } public void AddError(string Message) { Add(Message, System.Diagnostics.EventLogEntryType.Error); } public void AddWarning(string Message) { Add(Message, System.Diagnostics.EventLogEntryType.Warning); } public void AddInformation(string Message) { Add(Message, System.Diagnostics.EventLogEntryType.Information); } }

The Logger class has local variables to store a reference to the Windows event log, the source of the messages, and the log to which the messages will be written. In this example, the _Log variable is left empty, so all entries will go to the Application log by default. If you would like, you can experiment with this so that the entries are written to a different log. The constructor takes the name of the source application as a parameter. It creates an instance of the EventLog class and creates a new event source if it does not already exist. The Add method takes a message to write to the event log as well as the type for the entry and writes it to the event log. The AddError, AddWarning, and AddInformation methods are there for your convenience. Each method has a single parameter for the message to write to the event log. The methods then call Add with the appropriate entry type information. This keeps your calling code clean and readable.

I hope that, from this example, you can see how easy it is to write your own entries to the event log. There are many tools available that you then could use to help analyze the information sent to the event log by your application.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

Applied Knowledge, Inc.

714) 751-3480
1502 Brookhollow, Suite A
Santa Ana, CA
www.aki.com

Related Local Events
Windows Hardware Engineering Conference
Dates: 11/5/2008 - 11/7/2008
Location: Los Angeles Convention Center
Los Angeles CA
View Details

Digital Video Expo
Dates: 11/4/2008 - 11/6/2008
Location: Los Angeles Convention Center
Los Angeles CA
View Details

IT ChannelVision Fall 2008
Dates: 10/4/2008 - 10/8/2008
Location: Hyatt Regency Los Angeles
Los Angeles CA
View Details

IT ChannelVision Fall 2008
Dates: 10/4/2008 - 10/8/2008
Location: Hyatt Regency Los Angeles
Los Angeles CA
View Details

Biotechnology Vendor Showcase - University of California, Los Angeles
Dates: 10/2/2008 - 10/2/2008
Location: UCLA
Los Angeles CA
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Engineering Home Services Retail & Consumer Services
Business Services Entertainment Industrial Goods & Services Software
Career Family Insurance Technology
Cars Financial Services Internet Telecommunications
Computer Hardware Food & Beverage Legal Transportation & Logistics
Construction Health Pets Travel
Education Home Electronics Real Estate Wedding