The Best Places to Put .NET Application Logs

The Best Places to Put .NET Application Logs thumbnail
Application logs can sometimes help you resolve coding problems.

One of the best places to put .NET application logs is in the Windows Event Log that exists on every computer that uses Windows. Event logging, according to Microsoft, gives you the ability to store important information about your application's activity in a centralized location. When you need to review that information, you can do so manually or by writing a few lines of code that retrieve data from the Windows Event Log.

  1. Windows Event Viewer

    • Windows and third party applications write to the Windows Event Log continuously. View those log entries by typing "Administrative Tools" in your Start menu and selecting "Event Viewer" to open the Event Viewer program. The Application folder in the Event Viewer contains application log entries. Some developers may use application logs to capture information about errors that occur as a program runs. Others might log entries that record critical events such as database updates. One of the benefits of using the Windows Event log is the flexibility it gives you when cataloging event information. Each log record has fields such as EventType, EventID, Message and TimeWritten. After you set these values in your application, they appear in the Event Viewer immediately.

    Logging Events

    • You don't have to write much .NET code to create an entry in the Windows Event log. Microsoft .NET languages such as C# have a special EventLog class that handles reading, writing and even deletion for you. The following code creates a new instance of the EventLog class and writes the word "Warning" to it:

      if (!(EventLog.SourceExists("EventSource1", System.Environment.MachineName)))
      EventLog.CreateEventSource("EventSource1", "Application", System.Environment.MachineName);

      EventLog log1 = new EventLog("Application", System.Environment.MachineName, "EventSource1");

      log1.WriteEntry("Warning1", EventLogEntryType.Warning, 10001);
      log1.Close();

      EventLogEntryType.Warning tells Windows that you wish to log a warning message whose value is 10001. If you want to log an informational message instead, change EventLogEntryType.Warning to EventLogEntryType.Information. Place the WriteEntry statement at locations where you wish to log events, and .NET sends your information to the Windows Event Log.

    Reading Application Log

    • You can also read information programmatically using code similar to that shown in the following C# example:

      EventLog log1 = new EventLog("Application", System.Environment.MachineName);

      for (int i = log1.Entries.Count - 1; i < log1.Entries.Count; i--)
      {
      EventLogEntry entry = log1.Entries[i];
      Console.WriteLine(entry.EventID + " " + entry.Message + "\n");
      }
      log1.Close();

      The first line obtains a reference to the Application log, and the remaining code loops through the log and displays event IDs and messages. You can also view other event properties such as entry.TimeWritten and entry.EntryType. If you use Visual Studio to test your .NET applications, start the program in Administrative mode before running any code. This ensures that your application has the security privileges needed to access the Windows Event Log.

    Text Files

    • If you'd rather not use the Windows Event Log, create your own private log files and write anything you like to them using the .NET StreamWriter class. If you use this method, you must write the code needed to retrieve that data as well. You also lose the ability to view application log information quickly using the Event Viewer. Regardless of the method you use, remember that you do not have to log application events unless you really need to log errors or capture information that you may find useful later.

Related Searches

References

Resources

  • Photo Credit Comstock/Comstock/Getty Images

Related Ads

Featured
View Mobile Site