0

i am getting all information from windows security log and event viewer related to logon and logg off but i want only latest loggon event info from all information can you please apply some linq on that to get the top most Startup Login event info

here is my code what i am trying

      EventLog log = new EventLog()
        {
            Source = "Microsoft Windows security auditing.",
            Log = "Security"
        };
      foreach (EventLogEntry entry in log.Entries)
        {

            Console.WriteLine(entry.Message);
        }

can you make any foreach in lambda base to get only logon event that is the latest one

1 Answers1

1

Here is a sample to get the latest "Logon (4624)" and "Special Logon (4672)"

  var log = new EventLog
  {
    Source = "Microsoft Windows security auditing.",
    Log = "Security"
  };
  var latestLogon =
    log.Entries.Cast<EventLogEntry>()
      .Where(entry => entry.InstanceId == 4624 || entry.InstanceId == 4672)
      .OrderByDescending(i => i.TimeWritten)
      .FirstOrDefault();
  • DO YOU HAVE A LINK HOW TO START SERVICE AUTOMATICALLY ON PC RESTART ON LOGON C# – capricon december Jul 16 '16 at 10:23
  • Probably this one : http://stackoverflow.com/questions/1477618/how-do-i-change-a-windows-services-startup-type-in-net-post-install –  Jul 16 '16 at 10:25
  • done got it [start service on win login link](http://stackoverflow.com/questions/5089601/run-the-application-at-windows-startup) – capricon december Jul 16 '16 at 10:25
  • but the above code requires admin permission any solution for that on start up how to open run as on that function – capricon december Jul 16 '16 at 11:07
  • No, you need Admin to access "log.Entries". –  Jul 16 '16 at 11:16
  • [same prob part link](http://stackoverflow.com/questions/38410458/execute-specific-function-with-admistrator-preveligies-c) the link is to similar prob log enteris admin permission . how – capricon december Jul 16 '16 at 11:20
  • You need to add app.manifest like this : http://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator or start your visual studio using "run as administrator". Otherwise not possible. –  Jul 16 '16 at 11:23
  • IF I ADD APP CONFIG FILE LOGGIN FUNCTION NOT WORKS and if i use visual studio as admin what about my loggin event that is registered in registery to start on win start – capricon december Jul 16 '16 at 11:33
  • login event is in registery to start app automatically on win session login – capricon december Jul 16 '16 at 11:33
  • I dont understand what you want. Not app.config, but app.manifest. –  Jul 16 '16 at 11:37
  • Read that article carefully, not only add app.manifest, but you must change a line in that app.manifest `` –  Jul 16 '16 at 11:38
  • If you are creating windows service, then set it as Local System Account, read this : http://stackoverflow.com/questions/13082284/how-do-i-give-my-windows-service-admin-rights –  Jul 16 '16 at 11:40