3

when i compile the following code it shows the error as system.securityexception error.

using System;    
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace eventlog
{
   class MySample{

public static void Main(){

   if(!EventLog.SourceExists("MySource"))
    {            
        EventLog.CreateEventSource("MySource", "MyNewLog");
        Console.WriteLine("CreatedEventSource");
        Console.WriteLine("Exiting, execute the application a second time to use the source.");
        return;
    }        EventLog myLog = new EventLog();
    myLog.Source = "MySource";
   myLog.WriteEntry("Writing to event log.");

}

} }

how to fix this error

Gomathipriya
  • 905
  • 7
  • 19
  • 38
  • I copied your code and it compiles and runs well. What are the details of the exception? – codeteq Feb 01 '13 at 10:06
  • Unhandled Exception: System.Security.SecurityException: The source was not found , but some or all event logs could not be searched. Inaccessible logs: Security . at System.Diagnostics.EventLog.FindSourceRegistration(String source, String m achineName, Boolean readOnly) at System.Diagnostics.EventLog.SourceExists(String source, String machineName ) at eventlog.MySample.Main() in E:\.net prep\.net examples\eventlog\eventlog\P rogram.cs:line 13 The Zone of the assembly that failed was: MyComputer Press any key to continue . . . – Gomathipriya Feb 01 '13 at 10:08
  • please look at this question http://stackoverflow.com/questions/3622396/how-do-i-avoid-this-securityexception-when-writing-to-the-event-log – codeteq Feb 01 '13 at 10:11

3 Answers3

3

I think you must have Admin privilege for creating new EventLog.

Max
  • 6,821
  • 3
  • 43
  • 59
  • how to get that admin privilege – Gomathipriya Feb 01 '13 at 10:11
  • 1
    1. With Manifest file (http://msdn.microsoft.com/en-us/library/bb756929.aspx) - 2. Right Click on Exe (Run as Administrator) 3. Shortcut to exe, Property of shortcut (Run as Administrator) – Max Feb 01 '13 at 11:06
  • Check also this link (http://stackoverflow.com/questions/227187/uac-need-for-console-application) – Max Feb 01 '13 at 11:13
  • sorry to downvote but your answer is misleading. you CAN write to the EventLog without Admin privileges. You just cannot do what his code example does without admin privilege. See my "answer" (which I know does not completely answer how to do what he wants without admin privileges, but is a solution for somebody who just wants to write to it) – schmendrick Dec 22 '16 at 19:04
  • 2
    Have you read: CREATING not writing. Try yourself to create a NEW EventLog without Admin privilege.... And by the way this not a race everyone can post an answer that can be contribute to the solution. – Max Dec 23 '16 at 18:02
2

Read this article: http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx

That is why everybody says not write to eventlog with impersonation. You have to put the user or user group into the local Administrator group whitch is not advised. The service user could have this privilege.

Péter
  • 2,161
  • 3
  • 21
  • 30
0

To be more explicit, right click the application (exe) and choose "Run as Administrator"

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245