Using EventLog

M

Micro-Active

I'm trying to write to the eventlog but unfortunately I keep getting the
error at the bottom of this post. The code snippet is as follows:

string source = "My Program";
// Create the source, if it does not already exist.
if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, "Application");
}

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

// Write an informational entry to the event log.
myLog.WriteEntry("message");

The error occurs at the second line of code [ if
(!EventLog.SourceExists(source))]. This should be simple, shouldn't it?
I'm new to .NET 2.0/ VS 2005 and would be grateful for any hep you can
provide.

Regards

Mike



System.Security.SecurityException was unhandled
Message="The source was not found, but some or all event logs could not be
searched. Inaccessible logs: Security."
Source="System"
StackTrace:
at System.Diagnostics.EventLog.FindSourceRegistration(String source,
String machineName, Boolean readOnly)
at System.Diagnostics.EventLog.SourceExists(String source, String
machineName)
at System.Diagnostics.EventLog.SourceExists(String source)
at EventTest.Form1.button1_Click(Object sender, EventArgs e) in
C:\Users\Matthew\Documents\Visual Studio
2005\Projects\EventTest\EventTest\Form1.cs:line 29
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at EventTest.Program.Main() in C:\Users\Matthew\Documents\Visual
Studio 2005\Projects\EventTest\EventTest\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
K

Kevin Spencer

Offhand, I would guess that your application didn't have permission to read
and/or write to the System EventLog.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

Micro-Active said:
I'm trying to write to the eventlog but unfortunately I keep getting the
error at the bottom of this post. The code snippet is as follows:

string source = "My Program";
// Create the source, if it does not already exist.
if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, "Application");
}

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

// Write an informational entry to the event log.
myLog.WriteEntry("message");

The error occurs at the second line of code [ if
(!EventLog.SourceExists(source))]. This should be simple, shouldn't it?
I'm new to .NET 2.0/ VS 2005 and would be grateful for any hep you can
provide.

Regards

Mike



System.Security.SecurityException was unhandled
Message="The source was not found, but some or all event logs could not
be searched. Inaccessible logs: Security."
Source="System"
StackTrace:
at System.Diagnostics.EventLog.FindSourceRegistration(String source,
String machineName, Boolean readOnly)
at System.Diagnostics.EventLog.SourceExists(String source, String
machineName)
at System.Diagnostics.EventLog.SourceExists(String source)
at EventTest.Form1.button1_Click(Object sender, EventArgs e) in
C:\Users\Matthew\Documents\Visual Studio
2005\Projects\EventTest\EventTest\Form1.cs:line 29
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at EventTest.Program.Main() in C:\Users\Matthew\Documents\Visual
Studio 2005\Projects\EventTest\EventTest\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
T

Tony Wissler

Try this Change:

Current CODE: if (!EventLog.SourceExists(source))

Try: if (!EventLog.SourceExists("Application"))

Feel free to replace "Appication" with whatever event log you are trying to
write to:

Let me know how that works out for you.

Tony Wissler

tewissler
@gmail.com

Micro-Active said:
I'm trying to write to the eventlog but unfortunately I keep getting the
error at the bottom of this post. The code snippet is as follows:

string source = "My Program";
// Create the source, if it does not already exist.
if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, "Application");
}

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

// Write an informational entry to the event log.
myLog.WriteEntry("message");

The error occurs at the second line of code [ if
(!EventLog.SourceExists(source))]. This should be simple, shouldn't it?
I'm new to .NET 2.0/ VS 2005 and would be grateful for any hep you can
provide.

Regards

Mike



System.Security.SecurityException was unhandled
Message="The source was not found, but some or all event logs could not
be searched. Inaccessible logs: Security."
Source="System"
StackTrace:
at System.Diagnostics.EventLog.FindSourceRegistration(String source,
String machineName, Boolean readOnly)
at System.Diagnostics.EventLog.SourceExists(String source, String
machineName)
at System.Diagnostics.EventLog.SourceExists(String source)
at EventTest.Form1.button1_Click(Object sender, EventArgs e) in
C:\Users\Matthew\Documents\Visual Studio
2005\Projects\EventTest\EventTest\Form1.cs:line 29
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at EventTest.Program.Main() in C:\Users\Matthew\Documents\Visual
Studio 2005\Projects\EventTest\EventTest\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
Joined
Jul 28, 2008
Messages
1
Reaction score
0
I'm having a similar problem to Mike. Using vb.net, when this statement causes the same SecurityException
Dim b as Boolean = EventLog.SourceExists("MyApp")

Tony responded to Mike: >> Try: if (!EventLog.SourceExists("Application"))

This works ok for me, but two things:
Does this confuse the name of the log "Application" with the name of the source. Shouldn't I be able to name my
own sources (let alone test the EventLog for their existence.)

This doesn't appear to be a permissions issue. I am able to write to an existing log with an existing source name.
But if I try to test for a non-existent source name I get the SecurityException.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top