We can use Event log to store any error that our application generates as a result of testing or daily usage by the users. It is very useful when we are trying to log error messages which are events as well. The primary advantage is that we can see very detailed report of errors that occurred. This is much better mechanism of capturing errors since users may not always describe to us what the nature of the error is that they are getting via the interface. However, there are couple very serous considerations we have to take into account. First, Event Log must be run under very strict security framework. Second, Event Log is process intensive operation. Finally, Event Log can throw exceptions if Event Log is filled. Event log should be avoided with partial trust assemblies.
In order to create Event Log and write to it we need to use EventLog class.
public static void CreateEventLog()
{
public static void CreateEventLog()
{
DemoLog.Source = "Demo";
//WriteEntry
DemoLog.WriteEntry("CreateEventLog call", EventLogEntryType.Information);
} This will crate an entry in our list of Event Logs. If we want to remove Event Log that we just created we’ll do it the following waypublic static void DeleteEvantLog()
{
EventLog.Delete("Demo");
} EventLog is also useful to work with Built-in Event Logs such as Application Log, Security Log and System Log. Example of writing to Event Logpublic static void WriteToApplicationEventLog()
{
EventLog DemoLog = new EventLog("Application");
DemoLog.Source = "DemoApp";
DemoLog.WriteEntry("Written to Application Log", EventLogEntryType.Information);
} Reading from Event Log is very important if we try to display errors or failed attempt to run application to the web. We can do this using following code.public static void ReadEventLog()
{
EventLog MyDemoLog = new EventLog();
MyDemoLog.Log = "Demo";
foreach (EventLogEntry MyDemoEntry in MyDemoLog.Entries)
{
Console.WriteLine(MyDemoEntry.Source + ":" + MyDemoEntry.Message);
}
} Clearing Log is even simpler.public static void ClearEventLog()
{
EventLog MyLogDemo = new EventLog("Demo");
MyLogDemo.Source = "DemoApp";
MyLogDemo.Clear();
}
No comments:
Post a Comment