.NET Tip: Writing to the Event Log Washington

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

Local Companies

Essential Security Software
359-992-8926
11040 Main St
Bellevue, WA
Synergy Business Solutions
206-859-6507
221 First Avenue West
Seattle, WA
Avalara
206-780-7000
900 Winslow Way E
Bainbridge Island, WA
ERI Economic Research Institute
1.800.627.3697
8575 164th Ave. NE, Suite 100
Redmond, WA
ClearStar.net
360-892-0687
915 Broadway
Vancouver, WA
RDA
888-441-1278 x1313
18300 NE Union Hill Rd.
Redmond, WA
Vivonet Inc.
1-866-512-2033 ex.106
P.O. Box 2186
Bellingham, WA
Softalot LLC
206-850-9988
3931 NE 158TH LN
Seattle, WA
Veradis Technologies LLC
425.463.8819
3222 187th PL SE
Bothell, WA
Pacific Edge Software
425.897.8800
2606 116th Ave NE
Bellevue, WA

.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

WebOnyx

425-985-8802
900 1st Ave S.
Seattle, WA
www.webonyx.com

Regional Articles
- .NET Tip: Writing to the Event Log Anacortes WA
- .NET Tip: Writing to the Event Log Arlington WA
- .NET Tip: Writing to the Event Log Auburn WA
- .NET Tip: Writing to the Event Log Bainbridge Island WA
- .NET Tip: Writing to the Event Log Battle Ground WA
- .NET Tip: Writing to the Event Log Bellevue WA
- .NET Tip: Writing to the Event Log Bellingham WA
- .NET Tip: Writing to the Event Log Bothell WA
- .NET Tip: Writing to the Event Log Bremerton WA
- .NET Tip: Writing to the Event Log Buckley WA
- .NET Tip: Writing to the Event Log Camas WA
- .NET Tip: Writing to the Event Log Chehalis WA
- .NET Tip: Writing to the Event Log Cheney WA
- .NET Tip: Writing to the Event Log East Wenatchee WA
- .NET Tip: Writing to the Event Log Edmonds WA
- .NET Tip: Writing to the Event Log Ellensburg WA
- .NET Tip: Writing to the Event Log Enumclaw WA
- .NET Tip: Writing to the Event Log Everett WA
- .NET Tip: Writing to the Event Log Federal Way WA
- .NET Tip: Writing to the Event Log Gig Harbor WA
- .NET Tip: Writing to the Event Log Issaquah WA
- .NET Tip: Writing to the Event Log Kelso WA
- .NET Tip: Writing to the Event Log Kenmore WA
- .NET Tip: Writing to the Event Log Kennewick WA
- .NET Tip: Writing to the Event Log Kent WA
- .NET Tip: Writing to the Event Log Kirkland WA
- .NET Tip: Writing to the Event Log Lacey WA
- .NET Tip: Writing to the Event Log Lake Stevens WA
- .NET Tip: Writing to the Event Log Lakewood WA
- .NET Tip: Writing to the Event Log Longview WA
- .NET Tip: Writing to the Event Log Lynden WA
- .NET Tip: Writing to the Event Log Lynnwood WA
- .NET Tip: Writing to the Event Log Maple Valley WA
- .NET Tip: Writing to the Event Log Marysville WA
- .NET Tip: Writing to the Event Log Mercer Island WA
- .NET Tip: Writing to the Event Log Moses Lake WA
- .NET Tip: Writing to the Event Log Mount Vernon WA
- .NET Tip: Writing to the Event Log Mountlake Terrace WA
- .NET Tip: Writing to the Event Log Mukilteo WA
- .NET Tip: Writing to the Event Log Oak Harbor WA
- .NET Tip: Writing to the Event Log Olympia WA
- .NET Tip: Writing to the Event Log Pasco WA
- .NET Tip: Writing to the Event Log Port Angeles WA
- .NET Tip: Writing to the Event Log Port Orchard WA
- .NET Tip: Writing to the Event Log Poulsbo WA
- .NET Tip: Writing to the Event Log Pullman WA
- .NET Tip: Writing to the Event Log Puyallup WA
- .NET Tip: Writing to the Event Log Redmond WA
- .NET Tip: Writing to the Event Log Renton WA
- .NET Tip: Writing to the Event Log Richland WA
- .NET Tip: Writing to the Event Log Seattle WA
- .NET Tip: Writing to the Event Log Sedro Woolley WA
- .NET Tip: Writing to the Event Log Selah WA
- .NET Tip: Writing to the Event Log Sequim WA
- .NET Tip: Writing to the Event Log Shelton WA
- .NET Tip: Writing to the Event Log Silverdale WA
- .NET Tip: Writing to the Event Log Snohomish WA
- .NET Tip: Writing to the Event Log Spanaway WA
- .NET Tip: Writing to the Event Log Spokane WA
- .NET Tip: Writing to the Event Log Stanwood WA
- .NET Tip: Writing to the Event Log Sumner WA
- .NET Tip: Writing to the Event Log Tacoma WA
- .NET Tip: Writing to the Event Log University Place WA
- .NET Tip: Writing to the Event Log Vancouver WA
- .NET Tip: Writing to the Event Log Washougal WA
- .NET Tip: Writing to the Event Log Wenatchee WA
- .NET Tip: Writing to the Event Log Woodinville WA
- .NET Tip: Writing to the Event Log Yakima WA
- .NET Tip: Writing to the Event Log Yelm WA
Related Local Events
Tech Execs Seattle CIO Panel Forum
Dates: 1/9/2009 - 1/9/2009
Location: Qwest Field
Seattle WA
View Details

Tech Execs Seattle IT Solutions Conference and Showcase
Dates: 4/8/2009 - 4/8/2009
Location: Bell Harbor International Conference Center
Seattle WA
View Details

SpecOps West
Dates: 6/2/2009 - 6/4/2009
Location: Greater Tacoma Convention & Trade Center
Tacoma WA
View Details

Technology Convergence: The Integrated Lifestyle
Dates: 10/9/2008 - 10/9/2008
Location: Silverdale Beach Hotel
Silverdale WA
View Details

Seattle Cisco ASA Training: 2 Day Hands On Seminar
Dates: 10/1/2008 - 10/2/2008
Location: Holiday Inn Express SeaTac Airport
Seattle WA
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