.NET Tip: Writing to the Event Log

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)

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.


  • GHD jern beundrere vokser i antall og er forventet Ã¥ fortsette Ã¥ øke

    Posted by pletcherxsd on 06/14/2013 05:45am

    [url=http://www.rettetangnorgenews.net/]rettetang ghd[/url] Andre varianter eller serie av GHD rettetang inkluderer lilla GHD og GHD Precious Gift Set som kan gjøre en utmerket gave til noen spesiell på valentine, eller som en bursdagsgave. Akkurat på samme måte som du vil handle for andre varer over Internett for å få best mulig avtale, kan du også shoppe rundt på nettet for å få GHD billig hår straighteners.Whether du er en enkeltperson eller salong eier, er denne spesielle hår produktet en utmerket verktøy å ha. Som en saloon eier, kan du gå for GHD IV Salon Styler og andre spesielle serier for salongen. Men at du får intet mindre enn en ekte GHD rettetang. Hvis du blir tilbudt en GHD rettetang til en overraskende lav sats enn du forventer, må du tenke to ganger da dette kan være et rødt flagg for falske produktet. [url=http://ghdrettetangtilbud.moonfruit.com/]ghd rettetang tilbud[/url] Rettetang med den avanserte teknologien innen keratin, gjør at du beskytter den naturlige glansen i håret ditt mens du styler. Den har klar til bruk-indikator, digitale innstillinger med justerbar varmed for å få et jevnt resultat og et enkelt låsesystem for praktisk oppbevaring.Profesjonell slettetang med ekstra lange plater for maksimal effekt på langt/fyldig hår.Tilpass temperaturen etter håret ditt.Høyresistent keramisk belegg med tourmaline for beste resultat. [url=http://www.rettetangnewsnorge.com/falske-ghd]rettetang ghd[/url] De glitrende svarte platene skiller seg ut mot de rike rød, med avansert keramisk teknologi de glir jevnt gjennom håret gjør styling uanstrengt mens å gi håret den perfekte finish. Noen stil er oppnåelig fra rett og glatt til store bouncy krøller. De stilfulle utseende og strømlinjeformet GHD Rich Ruby Hair Straighteners vil gjøre styling lang og middels lengde hår hjemme til en lek – få at salongen ferdig til håret ditt når og hvor du vil.

  • Grab The Insiders Info Around mulberry bags Before You're Too Late

    Posted by hekvvddtest on 05/04/2013 07:59am

    It is well-known for its outdoor wear, but these days is much better known for their classically designed handbags. [url=http://nikeairmax.topvipshoes.co.uk/]nike air max[/url] [url=http://www.bymulberry.co.uk/]mulberry sale[/url] Although lines of Burberry UK started as outerwear, great patented raincoats and military wear over the years the famous checks dominated the skirts, the perfume boxes and presently the Burberry bags.

  • Gossip, Untruths Along With mulberry bags

    Posted by hekvvddtest on 05/03/2013 12:52pm

    If you want Birkenstock Sandals4less's professional version, take a look at the Birkenstock Sandals4lesss clogs licensed Alpro C203. [url=http://nikeairmax95.topvipshoes.co.uk/]nike air max 95[/url] [url=http://www.gocheapnikeairmax.co.uk/]nike air max 1[/url] Pro classics make a distinction with themselves by endurance, satisfying wearing relief, and versatility.

  • Originate 2013 a trendy color scheme Nike Air Max shoes

    Posted by Tufffruntee on 04/24/2013 08:58am

    Qualifying series Nike Breath Max HomeTurf metropolis recently irrevocably comes up, this series in the exemplary Air Max shoes to London, Paris and Milan [url=http://www.nikeskorrea.se/]air max dam[/url] the three paid tribute to the iconic city of Europe, combined with the characteristics of the three cities, Manner Max 1 HYP,Tune Max 90 HYP,Mood Max 1 and shoes such as Music pretension Max 95, combined with the Hyperfuse, as wonderfully as a medley of materials, such as suede, Whether you longing operational or retro-everything. It seems to me that more than Nike [url=http://www.nikeskorrea.se/nike-roshe-run-c-25/]nike roshe run[/url] shoes all star meeting sanctioned signature shoe series, and NSW series, all woman color layout of the chic Display Max Hyperposite exposure. Duct light brown hue, hyperfuse, swoosh, and are connected at the consecutively a the worst of the yellow/orange gradient snap details, lining is a Galaxy in [url=http://www.nikeskorrea.se/nike-air-max-2011-c-34/]nike air max 2011[/url] orange archetype garter, acutely beautiful.

  • I studied it already

    Posted by asalways on 05/01/2008 05:44am

    Amazing that I just too studied it weeks ago via a Japanese site

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date