Creating and deleting event logs with C# .NET

The Windows event viewer contains a lot of useful information on what happens on the system:

Windows event viewer

Windows will by default write a lot of information here at differing levels: information, warning, failure, success and error. You can also write to the event log, create new logs and delete them if the code has the EventLogPermission permission. However, bear in mind that it’s quite resource intensive to write to the event logs. So don’t use it for general logging purposes to record what’s happening in your application. Use it to record major but infrequent events like shutdown, severe failure, start-up or any out-of-the-ordinary cases.

There are some predefined Windows logs in the event log: Application, Security and System are the usual examples. However, you can create your own custom log if you wish. The key is the EventLog object located in the System.Diagnostics namespace:

string source = "DemoTestApplication";
string log = "DemoEventLog";
EventLog demoLog = new EventLog(log);
demoLog.Source = source;
demoLog.WriteEntry("This is the first message to the log", EventLogEntryType.Information);

The new log type was saved under the Applications and Services log category:

First custom log

You’ll probably have to restart the Event Viewer to find the new log type.

You can write to one of the existing Windows logs as well by specifying the name of the log. So we can create a log source within the Application log as follows:

string source = "DemoSourceWithinApplicationLog";
string log = "Application";
if (!EventLog.SourceExists(source))
{
	EventLog.CreateEventSource(source, log);
}
EventLog.WriteEntry(source, "First message from the demo log within Application", EventLogEntryType.Information);

The log entry is visible within the Application log:

Log entry in the Application log

It’s very easy to delete your custom log:

string log = "DemoEventLog";
EventLog.Delete(log);

The log will be deleted. Again, you’ll have to restart the event viewer to see that changes.

You can view all posts related to Diagnostics here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Creating and deleting event logs with C# .NET

  1. Gary Watson says:

    Is there a way of filtering the results as some of our eventlogs have hundreds of thousands of entrys and reading them takes a long time.
    What I’d like to do is present new “errors” from the event log when they occur,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: