Click here to Skip to main content
15,885,899 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello

I am fairly a beginner in coding and I have hit a snag.
I am developing a application that will monitor the security event log from startup and if there is a Logon Audit Failure event...my application will basically send a mail saying "Unauthorized access detected!"

my code snippet is :
C#
EventLog log = new EventLog("Security");
log.EnableRaisingEvents = true;

foreach (EventLogEntry entry in log.Entries)
{
    Console.WriteLine(entry.Message);
}
Console.ReadLine();

but every time i run this code there is Run time exception which says :

A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll

Additional information: Requested registry access is not allowed.


Can anybody point me towards a working solution?
please be as detailed as possible in your answer because I basically dont know anything and I want to know the internal workings of the code!

Thanks and Regards,
Kev
Posted
Updated 31-Oct-19 1:00am
v2
Comments
[no name] 13-Oct-15 10:00am    
Your code looks correct. You should have Admin right to that system so that you can access all log from Windows EventLog.
Richard MacCutchan 13-Oct-15 10:56am    
I basically dont know anything and I want to know the internal workings of the code!

This is the Quick Answers forum. If you want full details of the event logs and how to process them, then you need to use Google to search for samples and tutorials.
Richard Deeming 13-Oct-15 15:16pm    
Right-click on your application and select "Run as Administrator".

1 solution

Take a look at this (similar) question and the answer:

To access the Windows Security Log you need to have administrative privileges. Therefore, your app.manifest should be like this:

XML
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
      <security>
         <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
         </requestedPrivileges>
      </security>
   </trustInfo>
</asmv1:assembly>
 
Share this answer
 
v4
Comments
Richard MacCutchan 31-Oct-19 7:06am    
Four years too late dude.
Leo Chapiro 1-Nov-19 6:25am    
Funny, seems to be a bug of CP: I have not seen any comments of you and other guys as I answered yesterday! It looks like a new question apart of old creation date which I haven't noted :)
Richard MacCutchan 1-Nov-19 8:12am    
Yes, it was broken for a couple of days (see Bugs and Suggestions[^]) but seems to be working again now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900