Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EventLog.CreateEventSource is not creating a custom log

Tags:

c#

.net

logging

I have some code like this:

EventLog.CreateEventSource("myApp", "myAppLog");
EventLog.WriteEntry("myApp", "Test log message", EventLogEntryType.Error);

Now, unless I'm missing something having read MSDN, this should cause a new log 'myAppLog' to be created in the event viewer, and an entry should be added to that new log with the source name 'myApp'. But, I can't get the new log to be created. This always just writes an error log message to the Application log, with the source 'myApp' - 'myAppLog' is nowhere to be seen. What am I doing wrong? I am logged in as an Administrator.

like image 277
Jez Avatar asked Dec 14 '09 14:12

Jez


People also ask

How do I create a custom event log?

To create special log views, Click on the Administrative events. Click on Create Custom View on the right side of the window to open Create Custom View window. Under the Filter, there is Logged drop-down list. You can either choose an appropriate predefined time or use a custom time range for your Custom log views.

How do I create a new event log source?

To create an event source, you need to have a name for your new source (called the Event Source Name) and the name of the log where the event source will be a part. If the event log entries would be written to the standard “Application”, “System” or “Security” logs, then you can use that as the name of the log.

How do I create an event log in Event Viewer?

To generate these logs, please follow the steps listed below: Open "Event Viewer" by clicking the "Start" button. Click "Control Panel" > "System and Security" > "Administrative Tools", and then double-click "Event Viewer" Click to expand "Windows Logs" in the left pane, and then select "Application".

How do I create a custom event log for Windows service?

To set up logging to a custom log You must set AutoLog to false in order to use a custom log. Set up an instance of an EventLog component in your Windows Service application. Create a custom log by calling the CreateEventSource method and specifying the source string and the name of the log file you want to create.


2 Answers

Is it possible that you already used the source "myApp" when writing to the standard Application log? If so according to MSDN:

If a source has already been mapped to a log and you remap it to a new log, you must restart the computer for the changes to take effect.

http://msdn.microsoft.com/en-us/library/2awhba7a.aspx (about half way down the page)

like image 104
Chris Haas Avatar answered Sep 23 '22 12:09

Chris Haas


I have just written a little code to help me out of this. source registered in another log issue which I have encountered and don't want to manually have to remove sources from logs. What I decided to do was check if the source exists, if it does check that its linked to the correct log, if it isn't delete the source, now that it doesn't exist or f it never did create the Log brand new.

protected const string EventLogName = "MyLog";

private static bool CheckSourceExists(string source) {
  if (EventLog.SourceExists(source)) {
    EventLog evLog = new EventLog {Source = source};
    if (evLog.Log != EventLogName) {
      EventLog.DeleteEventSource(source);
    }
  }

  if (!EventLog.SourceExists(source)) {
    EventLog.CreateEventSource(source, EventLogName);
    EventLog.WriteEntry(source, String.Format("Event Log Created '{0}'/'{1}'", EventLogName, source), EventLogEntryType.Information);
  }

  return EventLog.SourceExists(source);
}

public static void WriteEventToMyLog(string source, string text, EventLogEntryType type) {      
  if (CheckSourceExists(source)) {          
      EventLog.WriteEntry(source, text, type);          
  }
}

Hopefully it helps :)

like image 44
PJUK Avatar answered Sep 19 '22 12:09

PJUK