Creating Event Log

  • Thread starter Thread starter Jerry C
  • Start date Start date
J

Jerry C

I am trying to create a new log in the event log system.

Using code:
EventLog.CreateEventSource("UAB","MPLog");

This does not create a new Log. The source is OK and appears in the
Application Log. What am I doing wrong.
 
Jerry said:
I am trying to create a new log in the event log system.

Using code:
EventLog.CreateEventSource("UAB","MPLog");

This does not create a new Log. The source is OK and appears in the
Application Log. What am I doing wrong.

Althoug you create the new log you also should specify it when writing
to (or opening) the event log.

Kornél
 
Hi Jerry,

As for wrting eventlog in .NET framework code. You need to both create an
EventSource(if it does not exists) and write a log entry into the certain
eventSource. The following code first ensure EventSource exists and then
write a log entry into it:

=================
static void WriteEventLog()
{
// Create the source, if it does not already exist.
if (!EventLog.SourceExists("UAB", Environment.MachineName))
{
EventSourceCreationData data = new
EventSourceCreationData("UAB", "MPLog");
data.MachineName = Environment.MachineName;
EventLog.CreateEventSource(data);

Console.WriteLine("CreatingEventSource: {0}", "UAB");
}

// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "UAB";

// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log, " + DateTime.Now);

Console.WriteLine("Message written to event log.");
}
=======================

To verify the log entry, you can open the event log viewr(eventvwr.exe).
And the "MPLog" log will appear under the root node.

Also, in windows vista, the viewer is a bit different, your custom log and
source will appear under "Applications and Services Logs" node.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.





-----------------
 
Steven,

Thank you for your reply.

I can creata the source and now the Log but the eventlog entry still goes
into the Application Log instead of the Log I created. This is a server 2008
machine and the Application Log is under Windows logs and the new log is
under Applications and services log.

I am using the code:

eventLogSource = "UFI";
eventLogName = "UFIEvents";

EventSourceCreationData UFIdata = new
EventSourceCreationData(eventLogSource, eventLogName);
UFIdata.MachineName = Environment.MachineName;
EventLog.CreateEventSource(UFIdata);

EventLog EventLogUFI = new EventLog(eventLogName);
EventLogUFI.Source = eventLogSource;
EventLogUFI.WriteEntry(message, EventLogEntryType.Error);

Even the line:

EventLog.LogNameFromSourceName(eventLogSource, Environment.MachineName)

returns the Name of the log UFIEvents.

What am I doing wrong.

By the way to view the new Log I have to restart the Event Viewer, refresh
does not work for Logs.

Thank you


--
Jerry


"Steven Cheng" said:
Hi Jerry,

As for wrting eventlog in .NET framework code. You need to both create an
EventSource(if it does not exists) and write a log entry into the certain
eventSource. The following code first ensure EventSource exists and then
write a log entry into it:

=================
static void WriteEventLog()
{
// Create the source, if it does not already exist.
if (!EventLog.SourceExists("UAB", Environment.MachineName))
{
EventSourceCreationData data = new
EventSourceCreationData("UAB", "MPLog");
data.MachineName = Environment.MachineName;
EventLog.CreateEventSource(data);

Console.WriteLine("CreatingEventSource: {0}", "UAB");
}

// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "UAB";

// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log, " + DateTime.Now);

Console.WriteLine("Message written to event log.");
}
=======================

To verify the log entry, you can open the event log viewr(eventvwr.exe).
And the "MPLog" log will appear under the root node.

Also, in windows vista, the viewer is a bit different, your custom log and
source will appear under "Applications and Services Logs" node.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.





-----------------
 
Hi Jerry,

Thanks for your reply,

I've just tested via the same code you pasted(on my Vista and Server 2008
machines)

=================
static void WriteLog()
{
string eventLogSource = "UFI";
string eventLogName = "UFIEvents";

if (!EventLog.SourceExists(eventLogSource,
Environment.MachineName))
{
EventSourceCreationData UFIdata = new
EventSourceCreationData(eventLogSource, eventLogName);
UFIdata.MachineName = Environment.MachineName;
EventLog.CreateEventSource(UFIdata);
}

EventLog EventLogUFI = new EventLog(eventLogName);
EventLogUFI.Source = eventLogSource;
EventLogUFI.WriteEntry("write log_"+DateTime.Now,
EventLogEntryType.Error);
}
===================

it seems the log entry is correctly written into the "UFIEvents" log under
" Applications and services log" instead of "windows-> application" log.
BTW, for creating EventSource, I need to restart eventViewer so as to see
the new eventsource, but for any written log entry, I can get them via
refreshing the eventviewer.

If necessary, I can send you my test project for your reference. Or you can
send me your test project so that I can test on my side. You can reach me
via the following address:

"stcheng" + @ + "microsoft.com"

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
 
Steven,

Thank you for your reply. I have emailed you please send me the project
maybe there is something in the config file that will help.

Thank you
 
Hi Jerry,

I've got your mail and have replied you with the test project. Please feel
free to let me know if you have any further progress or finding on it.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

--------------------
 
Steven,

Thank you for the code. Somehow the code started working last night. There
were several reboots and stuff while installing other stuff. Maybe that fixed
it. It probably was a permission problem don't know.
 
Thanks for your reply Jerry,

I'm glad to hear that. Yes, I think that also proves the problem to be
machine specific one and your original code should be ok.

Have a nice day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

--------------------
 
Back
Top