Answered by:
Event viewer custom log block

Question
-
Hi,
I am creating a Custom Log section block in EventViewer to log the events in Custom section rather then in Application Block. I am able to create the custom application section block, but the logs are still going in Application block. If i restart the machine it is working fine. I cant write the code which requires restarting the system. Here is the code that I used. (I am using this code in windows service)
public static void LogEventViewer(string message)
{
EventLog myLog = null;
if (!EventLog.Exists(logName))
{
EventLog.CreateEventSource("ABC.Windows.Service", "ABC");
}
EventLog[] logs = EventLog.GetEventLogs();
foreach (EventLog log in logs)
{
if (log.Log == "ABC")
{
myLog = log;
myLog.Source = "ABC.Windows.Service";
myLog.WriteEntry(message);
break;
}
}
}Please suggest. Thanks in advance.
Samagra
Tuesday, May 8, 2012 6:06 AM
Answers
-
Hi Centi,
I just found on MSDN http://msdn.microsoft.com/en-us/library/5zbwd3s3.aspx that "If a source is configured for an event log, and you reconfigure it for another event log, you must restart the computer for the changes to take effect."
Don't know if this is the only solution.
-Sam
Tuesday, May 8, 2012 10:12 AM
All replies
-
Hi,
you don't need to restart the computer just wait a couple of minutes because the operating system could take some time to refresh the event log list.
Bilhan silva
Tuesday, May 8, 2012 7:58 AM -
I think you are forgetting to set the Source property on your EventLog
if(!EventLog.SourceExists("
logName"))
logName
{
EventLog.CreateEventSource("","myLog");
}Also check the Answer to similar question.
- Edited by Zain_Ali Tuesday, May 8, 2012 9:17 AM
Tuesday, May 8, 2012 9:14 AM -
@Zain: I am setting the source property in the code see the foreach loop.
@Centigradz: I had waited 5 6 hours for this change but it didnt work.
Please advise if there is anything I am missing
Tuesday, May 8, 2012 10:00 AM -
Hi Centi,
I just found on MSDN http://msdn.microsoft.com/en-us/library/5zbwd3s3.aspx that "If a source is configured for an event log, and you reconfigure it for another event log, you must restart the computer for the changes to take effect."
Don't know if this is the only solution.
-Sam
Tuesday, May 8, 2012 10:12 AM